2018-05-31(表嚴肅)筆記四

一、路由

不需要每次更新頁面都重新取一次數(shù)據(jù),只需要取一次。

  1. 配置
    出現(xiàn)錯誤:將vue-router文件放在了js主文件的后面。調(diào)整過來錯誤消失。
var routes = [
    {
        path:'/',
         component: {
            template:`
            <div>
            <h1>首頁</h1>
            </div>
            `,
        },
    },
    {
        path:'/about',
         component: {
            template:
            `
            <div>
            <h1>關于我們</h1>
            </div>
            `,
        },
    },
    ];
     var router = new VueRouter({
          routes: routes,
        });
    new Vue({
        el:'#seg1',
        router:router,
    });
  1. 傳參
{
    path: '/user/:name',
    component: {
      template: `
      <div>
        <h1>我叫{{$route.params.name}}</h1>
        // 通過url里面的一部分
        <h1>我{{$route.query.age}}歲了</h1>
        // 通過URL中加?age=年齡來傳參
      </div>
      `,
    },
  },
  1. 子路由
    加載路由頁面的里面,component后面
{
        path: '/user/:name',
        component: {
          template: `
          <div>
            <h1>我叫{{$route.params.name}}</h1>
            <h1>我{{$route.query.age}}歲了</h1>
           <router-link :to="'/user/' + $route.params.name +'/more'">更多信息</router-link>
            <router-view></router-view>
          </div>
          `,
        },
         // <router-link to="more" append>更多信息</router-link>
        // <router-link :to="'/user' + $route.params.name +'/more'">更多信息</router-link>
        children:[
       {
        path: 'more',
        component: {
          template: `
          <div>
            用戶{{$route.params.name}}的詳細信息
            傻傻傻
          </div>
          `,
        },
      },
    ],
   },
  1. 手動訪問和傳參
    一個點擊方法:
        methods:{
          get:function () {
            setTimeout(function(){
              this.router.push('/about');
              setTimeout(function(){
                this.router.push({name:'user',params:{name:'王花花'}});
              },2000)
            },2000)
          }
        }
  1. 命名視圖
    頁面中的命名視圖最好只有兩個,多了不好維護
   <div>
       <router-link to="/">首頁</router-link>
       <router-link to="/user">用戶管理</router-link>
   </div>
   <div>
     <router-view name="sidebar"></router-view>
     <router-view name="content"></router-view>
   </div>
 {
        path: '/user',
        components: {
          sidebar:{
            template: `
          <div>
            <ul>
             <li>用戶列表</li>
             <li>權限管理</li>
            </ul>
          </div>
          `,
          },
          content:{
           template: `
          <div>
            用戶管理
          </div>
          `,
          }
        },
   },
  1. 導航鉤子
    檢查路由:
    beforeEach:將登陸值設置為false,
    如果已經(jīng)登陸,就正常跳轉,如果沒有就跳轉到登陸頁。
    afterEach:判斷一些信息
    router.beforeEach(function (to,from,next) {
     var logged = false;
     if(!logged && to.path == '/post')
      next('/login');
    else 
     next();
    });
    router.afterEach(function (to,from) {
   //判斷前一頁下一頁
    });
```-
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容