Vue.js学习笔记——路由实现选项卡简单实例

需要实现下图效果,点击上方选项卡,切换到不同内容的组件:

Vue.js学习笔记——路由实现选项卡简单实例_第1张图片

 

事先准备好两个库文件(vue.js、vue-router.js),放到对应路径。

1.引入依赖库


2.组件创建

    const Html = Vue.extend({
            template: '

html

{{msg}}

', data: function() { return { msg: 'This is the html vue-router.' } } }); const Css = Vue.extend({ template: '

CSS

{{msg}}

', data(){ return{ msg: 'This is the CSS vue-router.' } } }); const Vue1 = Vue.extend({ template: '

Vue

{{msg}}

', data(){ return{ msg: 'This is the Vue vue-router.' } } }); const Javascript = Vue.extend({ template: '

Javascript

{{msg}}

', data(){ return{ msg: 'This is the Javascript vue-router.' } } });

3.路由创建与映射

    const router = new VueRouter({
          routes: [
            { path: '/html', component: Html },
              { path: '/css', component: Css },
              { path: '/vue', component: Vue1 },
              { path: '/javascript', component: Javascript },
              { path: '/', component: Html } //默认路径
          ] 
        });

4.挂在实例

 const vm = new Vue({
              router: router 
        }).$mount('#app');

5.页面,to指令跳转到指定路径

html css vue javascript

6.所用样式

 

完整代码




    
    hello world
    
    

 

    
html css vue javascript

如有错误之处,欢迎指出,万分感谢!

 

 

你可能感兴趣的:(vue,路由,router,选项卡,导航,前端学习笔记)