admin管理员组文章数量:1391937
I'm reviewing Vue and it's router ponent, although I'm having some issues getting the router ponent to work. Error in console below:
Uncaught ReferenceError: router is not defined
Hi all,
I'm importing the Vue and VueRouter into an index.html
and trying my best to read the documentation to get the router initialized, but cannot seem to it working.
In index.html
:
<script type="module" src="/assets/js/main.js"></script>
<script src=".js"></script>
<script src=".js"></script>
In main.js
:
import VueRouter from 'vue-router'
Vue.use(VueRouter)
var router = new VueRouter({
routes: [
{ path: 'home', ponent: home }
]
});
var app = new Vue({
router,
el: '#app',
data: {
...etc
Help would be appreciated...
Many thanks.
I'm reviewing Vue and it's router ponent, although I'm having some issues getting the router ponent to work. Error in console below:
Uncaught ReferenceError: router is not defined
Hi all,
I'm importing the Vue and VueRouter into an index.html
and trying my best to read the documentation to get the router initialized, but cannot seem to it working.
In index.html
:
<script type="module" src="/assets/js/main.js"></script>
<script src="https://cdn.jsdelivr/npm/vue/dist/vue.js"></script>
<script src="https://unpkg./vue-router/dist/vue-router.js"></script>
In main.js
:
import VueRouter from 'vue-router'
Vue.use(VueRouter)
var router = new VueRouter({
routes: [
{ path: 'home', ponent: home }
]
});
var app = new Vue({
router,
el: '#app',
data: {
...etc
Help would be appreciated...
Many thanks.
Share Improve this question edited Aug 10, 2019 at 8:59 Andrew Vasylchuk 4,7792 gold badges13 silver badges31 bronze badges asked Aug 9, 2019 at 16:45 stoneferrystoneferry 1771 gold badge2 silver badges12 bronze badges 1-
If the second code sample is from
main.js
then I would suggest reordering your<script>
tags to putmain.js
last. Otherwise, could you include the full stacktrace for the error? – skirtle Commented Aug 9, 2019 at 17:00
2 Answers
Reset to default 3You can't use import VueRouter from 'vue-router'
AND <script src="https://unpkg./vue-router/dist/vue-router.js"></script>
import means it's expecting it as an npm dependency which will be bundled in the main.js
file when piled.
you need to run npm install -save vue-router
or remove the import ...
statement.
Seems like you do not have any building process, and you load Vue and Vue Router directly in browser.
To make everything work, you should follow next steps:
- Change the order of scripts, so your
main.js
goes last. Because you need Vue and Vue Router already be loaded before you init your application.
<script src="https://cdn.jsdelivr/npm/vue/dist/vue.js"></script>
<script src="https://unpkg./vue-router/dist/vue-router.js"></script>
<script type="module" src="/assets/js/main.js"></script>
- Remove
import
from yourmain.js
as it runs directly in browser.
Vue.use(VueRouter)
var router = new VueRouter({
routes: [
{ path: 'home', ponent: home }
]
});
var app = new Vue({
router,
el: '#app',
data: {
...etc
本文标签: javascriptvuerouter Uncaught ReferenceError router is not definedStack Overflow
版权声明:本文标题:javascript - vue-router Uncaught ReferenceError: router is not defined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744669891a2618761.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论