尝试通过vue.js和vue-router.js演示路由,但收到SyntaxError“无法在模块外部使用import语句”。我正在尝试从正确的位置导入vue。下面是树的打印输出。我是否无法正确导入Vue?谢谢。
树打印输出:
.
├── index.html
├── index.js
├── js
│ ├── vue-router.js
│ └── vue.js
└── src
├── App.vue
├── AppRouter.vue
├── ArticlesPage.vue
└── HomePage.vue
index.html
<!DOCTYPE html>
<html>
<head>
<title>Vue app</title>
</head>
<body>
<div id="app">
</div>
</body>
<script src="js/vue.js"></script>
<script src="js/vue-router.js"></script>
<script src="index.js"></script>
</html>
index.js
import Vue from "./js/vue"; // errors here
import App from "./src/App";
const app = new Vue({
el: "#app",
render(h) {
return h(App);
}
});
答案 0 :(得分:1)
.
├── package.json
├── public
│ ├── index.html
│ └── favicon.ico
└── src
├── assets
│ └── logo.png
├── components
│ └── HelloWorld.vue
├── router
│ └── index.js
├── views
│ └── Home.vue
│ └── About.vue
├── App.vue
└── main.js
import Vue from 'vue';
import App from './App.vue';
import router from './router';
Vue.config.productionTip = false;
new Vue({
router,
render: h => h(App)
}).$mount('#app');
import Vue from 'vue';
import VueRouter from 'vue-router';
Vue.use(VueRouter);
const routes = [
{
path: '/',
name: 'home',
component: () => import('../views/Home.vue')
},
{
path: '/about',
name: 'about',
component: () => import('../views/About.vue')
}
];
const router = new VueRouter({
mode: 'history',
routes
});
export default router;
<template>
<div id="app">
<router-link :to="{ name: 'home' }">HOME</router-link>
<router-link :to="{ name: 'about' }">ABOUT</router-link>
<router-view />
</div>
</template>
<script>
export default {
name: "App",
};
</script>
<template>
<div id="home">
<img width="25%" src="../assets/logo.png">
<HelloWorld msg="Hello Vue from Home page!"/>
</div>
</template>
<script>
import HelloWorld from "../components/HelloWorld";
export default {
name: "Home",
components: {
HelloWorld
}
};
</script>
答案 1 :(得分:0)
这是因为您以独立方式运行。 在这种情况下,您需要在脚本标记上添加type =“ module”。
<script src="index.js" type="module"></script>