【uniapp】vue-cli微信小程序使用uni-simple-router

1. 安裝

npm install uni-simple-router

2. 根據(jù)官網(wǎng)介紹,選擇了把router分成多個文件,具體如下

官網(wǎng)介紹

example 模塊和 home 模塊,index.js是匯集模塊

image.png

home文件(example.js文件類似)

const home = [
  {
    path: '/pages/home',
    aliasPath: '/',  //對于h5端你必須在首頁加上aliasPath并設(shè)置為/
    name: 'home',
    meta: {
      title: '首頁',
    }
 },
 {
    path: '/pages/modules/sys/login',
    name: 'login',
    meta: {
      title: '登錄',
    },
  },
...
];
export default home

3. 在main.js中添加引用 (@表示src目錄)

import router from '@/router'

Vue.use(router);


image.png

4. 目前還缺少一步配置??!

在項目最外層(與src同目錄)建立vue.config.js文件(此文件默認(rèn)不創(chuàng)建)

module.exports = {
  transpileDependencies:['uni-simple-router']
}

5. 測試

創(chuàng)建以上router中的三個頁面,同樣在pages.json中聲明

  "pages": [
    {
      "path": "pages/home",
      "style": {
        "navigationBarTitleText": "首頁",
        "navigationStyle": "custom"
      }
    },
    {
      "path": "pages/modules/sys/mine",
      "style": {
        "navigationBarTitleText": "我的",
        "navigationStyle": "custom"
      }
    }
  ]

home頁面添加按鈕,測試跳轉(zhuǎn)

<template>
    <view>
        <tui-button @click="toPage">測試按鈕</tui-button>
    </view>
</template>

<script>
  import TuiButton from "@/components/button/button";
  export default {
    components: {
      TuiButton
    },
    methods: {
        toPage() {
        this.$Router.push({name: 'login'})
        }
    }
  }
</script>

<style scoped>
</style>

至此,路由設(shè)置基本完畢,具體跳轉(zhuǎn)方式及路由攔截請參照官網(wǎng)。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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