gpt4 book ai didi

javascript - Vue-Router 返回 "Cannot redefine property: $router"错误

转载 作者:行者123 更新时间:2023-11-30 14:12:08 24 4
gpt4 key购买 nike

我正在使用 vue.js 创建一个小网站,我正在尝试设置一个 vue-router(到目前为止还没有成功)。我收到“无法重新定义属性:$router”错误。这是我的代码。应用程序 View

<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>

主要.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>

我之前使用过 vue-router 并遇到了这个问题。我觉得我错过了什么,但我不知道是什么,我们将不胜感激任何帮助

最佳答案

您有重复的 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')

关于javascript - Vue-Router 返回 "Cannot redefine property: $router"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54223152/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com