路由模块化
router.js
import Vue from 'vue'; //配置路由 import VueRouter from 'vue-router'; Vue.use(VueRouter); //1.创建组件 import Home from '../components/Home.vue'; import News from '../components/News.vue'; import User from '../components/User.vue'; //2.配置路由 注意:名字 const routes = [ { path: '/home', component: Home }, { path: '/news', component: News, name: 'news' }, { path: '/user', component: User }, { path: '*', redirect: '/home' } /*默认跳转路由*/ ] //3.实例化VueRouter 注意:名字 const router = new VueRouter({ mode: 'history', /*hash模式改为history*/ routes // (缩写)相当于 routes: routes }) //5 <router-view></router-view> 放在 App.vue export default router;
import Vue from 'vue';
import App from './App.vue';
//引入公共的scss 注意:创建项目的时候必须用scss
import './assets/css/basic.scss';
import router from './router/router.js';
//4、挂载路由
new Vue({
el: '#app',
router,
render: h => h(App)
})
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!