im使用vue.js创建一个小型站点,尝试安装vue-router(到目前为止还没有成功)。即时通讯收到“无法重新定义属性:$ router”错误。 这是我的代码。 App.Vue
<div id="app">
<div class="header">
<div class="tab">
<router-link to="/page1">page1</router-link>
</div>
<div class="tab">
<router-link to="/page2">page2</router-link>
</div>
<div class="tab">
<router-link to="/page3">page3</router-link>
</div>
<div class="tab">
<router-link to="/page4">page4</router-link>
</div>
</div>
<div>
<router-view></router-view>
</div>
Page1.vue
<template>
<div>
<h3>sup</h3>
</div>
</template>
<script>
export default {
name: 'app',
data () {
return {
msg: 'hello world'
}
}
}
</script>
<style>
</style>
main.js
import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
new Vue({
el: '#app',
render: h => h(App)
})
const Page1 = { template: '<div>Page1</div>' }
const Page2 = { template: '<div>Page2</div>' }
const routes = [
{ path: '/page1', component: Page1 },
{ path: '/page2', component: Page1 }
]
const router = new VueRouter({
routes // short for `routes: routes`
})
const app = new Vue({
router
}).$mount('#app')
index.html
<!DOCTYPE html>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<html lang="en">
<head>
<meta charset="utf-8">
<title>hello-world</title>
</head>
<body>
<div id="app"></div>
<script src="/dist/build.js"></script>
</body>
</html>
ive之前曾与vue-router合作,并遇到了这个问题。我觉得自己缺少了一些东西,但我不知道该怎么办,将不胜感激
答案 0 :(得分:0)
您的主要Vue实例已复制:
new Vue({
el: '#app',
render: h => h(App)
})
和:
const app = new Vue({
router
}).$mount('#app')
我认为这是问题的根源。我想它看起来像这样:
import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const Page1 = { template: '<div>Page1</div>' }
const Page2 = { template: '<div>Page2</div>' }
const routes = [
{ path: '/page1', component: Page1 },
{ path: '/page2', component: Page1 }
]
const router = new VueRouter({
routes // short for `routes: routes`
})
const app = new Vue({
router,
render: h => h(App)
}).$mount('#app')
答案 1 :(得分:0)
您有重复的Vue实例。
检查。
import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const Page1 = { template: '<div>Page1</div>' }
const Page2 = { template: '<div>Page2</div>' }
const routes = [
{ path: '/page1', component: Page1 },
{ path: '/page2', component: Page1 }
]
const router = new VueRouter({
routes // short for `routes: routes`
})
const app = new Vue({
router,
render: h => h(App)
}).$mount('#app')