1. vue-Roter基本使用 及 路由傳遞參數(shù)教程 和 table表單 的結(jié)構(gòu)

vue-Roter 介紹

1. 注意語法: 多練習(xí),才能避免一些語法錯(cuò)誤


2. vue-Router介紹

   // 定義組件
   const User = {
    template: '<h1>User 組件 -- 用戶的id為: {{$route.params.id}} </h1>'
  }
  
  // 模板字符串 ``
  const Register = {
    template: `
    <div>
      <h1>Register 組件</h1>
      <!-- 分割線 -->
      <hr/>

      <!-- 子路由 -->
      <router-link to = "/register/tab1">tab1</router-link>

      <!-- 子占位符 -->
      <router-view></router-view>
    </div>
    `
  }

  const Tab1 = {
    template: '<h3>Tab1 組件</h3>'
  }
  
  // 創(chuàng)建路由實(shí)例對象
  const router = new VueRouter({
    // 所有的路由規(guī)則
    routes: [
      // 路由重定向
      {path: '/', redirect: '/user'},
      // 動態(tài)路由 參數(shù)傳遞
      {path: '/user/:id', component: User},
      // children 數(shù)組表示子路由規(guī)則
      { path: '/register', component: Register, children: [
        {path: '/register/tab1', component: Tab1},
        {path: '/register/tab2', component: Tab2}
      ]}
    ]
  })

  // 創(chuàng)建 vm 實(shí)例對象
  const vm = new Vue({
    // 指定控制的區(qū)域
    el: '#app',
    data: {},
    // 掛載路由實(shí)例對象  es6 新語法 當(dāng)兩個(gè)屬性名稱完全一樣時(shí) 可以簡寫為一個(gè)
    // router: router
    router
  })


3. 注意 很重要

  • router 是 路由實(shí)例對象
  • routes 是 實(shí)例對象的規(guī)則: 必須要有 path 和 component
  • route 是 獲取路由的參數(shù)


4. vue 動態(tài)路由傳參

// 方式一
const User = {
        template: '<h1>User 組件 -- 用戶id為: {{$route.params.id}}</h1>'
      }

{ path: '/user/:id', component: User },
    
// 方式二  最常用的方法
    const User = {
        props: ['id'],
        template: '<h1>User 組件 -- 用戶id為: {{id}}</h1>'
      }
    
    { path: '/user/:id', component: User, props: true },
        
// 方式三
        const User = {
        props: ['id', 'uname', 'age'],
        template: '<h1>User 組件 -- 用戶id為: {{id}} -- 姓名為:{{uname}} -- 年齡為:{{age}}</h1>'
      }
        
        // 這里訪問不到 id 的值
        { path: '/user/:id', component: User, props: { uname: 'lisi', age: 20}},
        
// 方式四
        const User = {
        props: ['id', 'uname', 'age'],
        template: '<h1>User 組件 -- 用戶id為: {{id}} -- 姓名為:{{uname}} -- 年齡為:{{age}}</h1>'
      }
        
        // 將 props 設(shè)置為 箭頭函數(shù)
        {
            path: '/user/:id',
            component: User,
            props: route => ({ uname: 'zs', age: 20, id: route.params.id})
          },


5. 命名路由

<router-link :to="{ name: 'user', params: {id: 3} }">User3</router-link>

{
            // 命名路由
            name: 'user',
            path: '/user/:id',
            component: User,
            props: route => ({ uname: 'zs', age: 20, id: route.params.id })
          },


6. vue-router 編程式導(dǎo)航

this.$router.push('/register')
// -1 表示返回上一頁   1  表示前進(jìn)一頁
this.$router.go(-1)


const User = {
        props: ['id'],
        template: `<div>
          <h1>User 組件 -- 用戶id為: {{id}} </h1>
          <button @click="goRegister">跳轉(zhuǎn)到注冊頁面</button>
        </div>`,
        methods: {
          goRegister() {
            this.$router.push('/register')
          }
        },
      }

      const Register = {
        template: `<div>
          <h1>Register 組件</h1>
          <button @click="goBack">后退</button>
        </div>`,
        methods: {
          goBack() {
            this.$router.go(-1)
          }
        }
      }
      
      
// router.push()傳遞參數(shù)的方法
// 1. 字符串(路徑名稱) 最常用
      this.$router.push('/home')

// 2.對象
this.$router.push({ params: '/home' })

// 3. 命名式傳遞參數(shù)
this.$router.push({ name: '/user', params: { userId: 123 }})

// 4. 帶查詢參數(shù) 變成 register?uname=lisi
this.$router.push({ path: '/register', query: { uname: 'lisi' }})


7. 表單

<table>
            <thead>
              <tr>
                <th>編號</th>
                <th>姓名</th>
                <th>年齡</th>
                <th>
                  操作
                </th>
              </tr>  
            </thead>  
            <tbody>
              <tr v-for="item in userlist" :key="item.id">
                <td>{{item.id}}</td>
                <td>{{item.name}}</td>
                <td>{{item.age}}</td>
                <td><a href="javascript:;">詳情</a></td>
              </tr>  
            </tbody>
          </table>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • 16宿命:用概率思維提高你的勝算 以前的我是風(fēng)險(xiǎn)厭惡者,不喜歡去冒險(xiǎn),但是人生放棄了冒險(xiǎn),也就放棄了無數(shù)的可能。 ...
    yichen大刀閱讀 7,697評論 0 4
  • 公元:2019年11月28日19時(shí)42分農(nóng)歷:二零一九年 十一月 初三日 戌時(shí)干支:己亥乙亥己巳甲戌當(dāng)月節(jié)氣:立冬...
    石放閱讀 7,413評論 0 2
  • 今天上午陪老媽看病,下午健身房跑步,晚上想想今天還沒有斷舍離,馬上做,衣架和旁邊的的布衣架,一看亂亂,又想想自己是...
    影子3623253閱讀 3,061評論 3 8

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