【发布时间】:2018-06-24 10:50:55
【问题描述】:
每个人。
我对制作vue组件有个小疑问。
我不想使用 browserify 或 webpack ,因为我在 django 工作,它的大部分模板都在静态文件中,尽管我读过 @987654321 @ ,它确实描述了如何同时考虑两者(但那是其他日子)。
问题:
我正在制作一个单个文件组件,我必须使用我的路由器导入和使用它,但我不能,因为导入不会发生。
我的Hello.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.<script src="../components/Hello.js" type="module"></script> 并按照建议删除导入语句here
- 用这个替换我的 Hello.js 的代码:
export const Hello = { ... - 制作一个 Hello.js 文件并像这样导入它
import Hello from '../components/Hello.js';
错误:
- **Mozilla(量子 57.0.4 64 位)**:
SyntaxError: import declarations may only appear at top level of a module - **Chrome ( 63.0.3239.108 (官方版本) (64-bit) ) ** :
Uncaught SyntaxError: Unexpected identifier
【问题讨论】:
-
你是如何使用所有这些 ES6 实现和转译开始的?
-
ES6 import/export 和 CommonJS module.exports 都不能在任何浏览器中工作。前者需要 Webpack + Babel,后者需要 Browserify。
-
另外,我在这里没有看到任何single file components,到目前为止你只有常规组件。如果您确实拥有它们,那么您肯定需要带有适当加载器的 Webpack。
-
@Alex 那是我不想使用的,因为我有 django 后端,它对所有这些都使用静态文件/文件夹
-
@jame “编译”是 webpack 或 browserify 或类似工具的工作。你要求相反的东西。你不能有一个单一的文件组件而不使用 webpack 或类似的东西。 You'll have to register your components programatically 然后使用脚本标签包含它们。
标签: javascript vue.js vuejs2