所有人。
我对制作vue组件有一个小小的怀疑。
我不想使用browserify或webpack,因为我使用的是django,它的大部分模板都在静态文件中,尽管我读过这篇文章,其中描述了如何兼顾两者(但那是以后的事情了)。
我正在制作一个文件组件,我必须导入和使用,使用我的路由器,但我不能,因为导入只是没有发生。
你好Vue
<template>
Some HTML code here.
</template>
<script>
module.exports = {
data() {
return {
coin : []
}
},
beforeRouteEnter (to, from, next) {
axios.get('my-django-rest-api-url')
.then(response => {
next(vm => {
vm.data = response.data
})
})
}
}
</script>
我把它放在index.html文件中,而不是其他.js文件中,
<script>
import Hello from '@/components/Hello.vue'
Vue.use(VueRouter);
const dashboard = {template:'<p>This is the base template</p>'};
const profile = {
template: '#profile_template',
data () {
return {
profile_details: []
}
},
beforeRouteEnter (to, from, next) {
axios.get('my-api-url')
.then(response => {
next(vm => {
vm.profile_details = response.data
})
})
}
}
const router = new VueRouter({
routes: [
{ path: '/', component: dashboard },
{ path: '/profile', component: profile },
{ path: '/hello', component: Hello }
]
});
new Vue({
router : router,
}).$mount('#app');
</script>
1.并按此处建议删除import语句
语法错误:导入声明只能出现在模块的顶层
未捕获的语法错误:意外的标识符
不是Vue.js大师,但这里有几个观点可能会对您有所帮助。
导入
和导出
,则需要WebPack。当然还有Babel(或任何其他ES6 transpiler,例如Buble)module.exports
,那么您需要Browserify。它支持浏览器环境中的CommonJS。
导入每个文件。绝对不是最干净的方法。.vue
文件中,但无论哪种方式,它们都需要vue-loader
,可以使用绑定程序添加和配置该组件。
s.
你试图做一些不可能的事情。web浏览器不支持单个文件组件作为原始组件文件。应该编译单个文件组件。
更多信息请参阅:https://vuejs.org/v2/guide/single-file-components.html
在Webpack中,每个文件在包含在包中之前都可以通过“加载器”进行转换,Vue提供了vue-loader插件来转换单文件(.Vue)组件。
vue单个文件组件首先被“翻译”(编译)为浏览器可以使用的纯javascript代码。