代码是狂神说java14集vue-router路由 po在下面
App.vue
<template>
<div id="app">
<h1>vue-router</h1>
<router-link to="/main">首页</router-link>
<router-link to="/content">内容页</router-link>
<router-view></router-view>
</div>
</template>
<script>
export default {
name: 'App',
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
main.js
import Vue from 'vue'
import App from './App'
import router from './router'//自动扫描路由配置
Vue.config.productionTip = false;
new Vue({
el: '#app',
//配置路由
router,
components: { App },
template: '<App/>'
})
index.js
import Vue from 'vue'
import VueRouter from 'vue-router'
import Content from '../components/Content'
import Main from '../components/Main'
//安装路由
Vue.use(VueRouter);
//配置导出路由
export default new VueRouter({
routes:[
{
//路由路径
path:'/content',
//跳转的组件
name:"content",
component:Main
}
]
});
Content.vue
<template>
<h1>内容页</h1>
</template>
<script>
export default{
name:"Content"
}
</script>
<style>
</style>
运行的时候会产生 Failed to load resource: the server responded with a status of 404 (Not Found)错误警告
根据博主的建议将打包位置更改
path:'./content'
后变成404警告
后期重新打开还是之前的错误警告
还有一个博主说组件的名字要跟代码名字相同
目前没有找到解决办法,后期有的话会更在评论区。
|