淺談vue-router路由傳參的方法

路由傳參數(shù)。在很多時(shí)候我們需要路由上面?zhèn)鬟f參數(shù),比如新聞列表頁,我們需要傳遞新聞ID,給新聞詳細(xì)頁。

站在用戶的角度思考問題,與客戶深入溝通,找到西安網(wǎng)站設(shè)計(jì)與西安網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站建設(shè)、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、國際域名空間、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋西安地區(qū)。

1.新聞列表頁模板

 <template id="news">
    <div>
      <h3>新聞列表</h3>
      <ul>
        <li>
          <router-link to="/news/001">新聞001</router-link>
        </li>
        <li>
          <router-link to="/news/002">新聞002</router-link>
        </li>
      </ul>      
    </div>
  </template>

我們訪問/news/001,跳轉(zhuǎn)到新聞詳細(xì)頁,展示001的這條新聞。

2.新聞詳細(xì)頁組件準(zhǔn)備

 <template id="NewsDetail">
    <div>
      新聞詳細(xì)頁面
      <span>{{$route.params.id}}</span>
    </div>
  </template>

$route.params.id獲取路由上的參數(shù)

3.定義路由,增加一個(gè)路由

{ path: '/news/:id', component: NewsDetail },

4.全部代碼如下:

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <meta charset="utf-8">
  <script src="http://unpkg.com/vue/dist/vue.js"></script>
  <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
<body> 
  <div id="box">
    <p>
      <router-link to="/home">home</router-link>
      <router-link to="/news">news</router-link>
    </p>
     <router-view></router-view>
  </div>

  <!-- 模板抽離出來 -->
  <template id="home">
    <!-- 注意:組件只能有一個(gè)根元素,所以我們包裝到這個(gè)div中 -->
    <div>
      <h3>首頁</h3>
       <router-link to="/home/login">登錄</router-link>
      <router-link to="/home/reg">注冊</router-link>
      <!-- 路由匹配到的組件將渲染在這里 -->
      <router-view></router-view>
    </div>
  </template>

  <template id="news">
    <div>
      <h3>新聞列表</h3>
      <ul>
        <li>
          <router-link to="/news/001">新聞001</router-link>
        </li>
        <li>
          <router-link to="/news/002">新聞002</router-link>
        </li>
      </ul>      
    </div>
  </template>

  <template id="login">
    <div>登錄界面</div>
  </template>
  <template id="reg">
    <div>注冊界面</div>
  </template>

  <template id="NewsDetail">
    <div>
      新聞詳細(xì)頁面
      <span>{{$route.params.id}}</span>
    </div>
  </template>

  <script type="text/javascript">
    // 1. 定義(路由)組件。
    const Home = { template: '#home' };
    const News = { template: '#news' };

    const Login = { template: '#login' };
    const Reg = { template: '#reg' };

    //新聞詳細(xì)頁組件
    const NewsDetail = { template: '#NewsDetail' };


    // 2. 定義路由
    const routes = [
       { path: '/', redirect: '/home' },
      { 
        path: '/home', 
        component: Home, 
        children:[
          { path: '/home/login', component: Login},
          { path: '/home/reg', component: Reg}
        ]
      },
      { path: '/news', component: News,},
      { path: '/news/:id', component: NewsDetail },

    ]

    // 3. 創(chuàng)建 router 實(shí)例,然后傳 `routes` 配置
    const router = new VueRouter({
      routes // (縮寫)相當(dāng)于 routes: routes
    })


    // 4. 創(chuàng)建和掛載根實(shí)例。
    // 記得要通過 router 配置參數(shù)注入路由,
    // 從而讓整個(gè)應(yīng)用都有路由功能
    const app = new Vue({
     router
    }).$mount('#box')

    // 現(xiàn)在,應(yīng)用已經(jīng)啟動(dòng)了!
  </script>
</body>
</html>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

標(biāo)題名稱:淺談vue-router路由傳參的方法
轉(zhuǎn)載注明:http://muchs.cn/article18/ihjdgp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、Google響應(yīng)式網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、企業(yè)建站、域名注冊

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)