亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue3-vue-router創(chuàng)建靜態(tài)路由和動(dòng)態(tài)路由方式

 更新時(shí)間:2023年10月23日 09:08:31   作者:qq_41387775  
這篇文章主要介紹了vue3-vue-router創(chuàng)建靜態(tài)路由和動(dòng)態(tài)路由方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

根據(jù)官網(wǎng)代碼搭建router遇到了很多問(wèn)題

 html

是指用于HTML文件里的

javascript

卸載js文件里

Vue-cli項(xiàng)目中使用router

1.創(chuàng)建router.js文件

在router.js文件里創(chuàng)建路由, views文件加下面存放路由跳轉(zhuǎn)的頁(yè)面。

遇到的一些語(yǔ)法問(wèn)題: export 導(dǎo)出的屬性import時(shí)需要用花括號(hào), export default 不需要花括號(hào), 如下方的createWebHistory放在花括號(hào)里才能用

// router/index
import { createWebHistory, createRouter } from "vue-router";
import Home from "../views/home.vue";
import About from "../views/about.vue";
import dynamicPage from "../views/dynamicHome.vue"

const routes = [
  {
    path: "/",
    name: "home",
    component: Home,
  },
  {
    path: "/about",
    name: "About",
    component: About,
  },
  {
    path: "/dynamicPage/:id",
    name: "dynamicPage",
    component: dynamicPage,
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

export default router;
// view/home.vue
<template>
  <div class="home">這是首頁(yè)首頁(yè)</div>
</template>

// view/about
<template>
  <div class="home">這是詳情頁(yè)</div>
</template>

2.main.ts文件創(chuàng)建app時(shí)安裝路由插件

這是vue3的用法,use安裝插件時(shí)跟在createApp創(chuàng)建的實(shí)例后面

import { createApp } from "vue";
import App from "./App.vue";
import router from "./router"

createApp(App).use(router).mount("#app")

3.vue頁(yè)面應(yīng)用組件

// singleRouter
<template>
  <div class="single-router">
    <h3>路由跳轉(zhuǎn)</h3>
    <button @click="gotoAbout">go to About</button>
    <div>
      <router-link to="/">首頁(yè)</router-link> |
      <router-link to="/about">關(guān)于</router-link>
    </div>
    <h4>路由內(nèi)容</h4>
    <router-view />
    <h4>結(jié)束</h4>
  </div>
</template>
<script>
export default {
  name: "singleRouter",
  components: {},
  setup() {},
  methods: {
    gotoAbout() {
      const router = this.$router;
      router.push("/about");
      return {};
    },
  },
};
</script>
<style scoped>
.single-router {
  background-color: #cccccc;
}
</style>

動(dòng)態(tài)路由

1.配置動(dòng)態(tài)路由

router/index.js文件里設(shè)置路由時(shí),路徑后面加/:id,組件可以通過(guò)id獲取,id可以取為任何名字

2.動(dòng)態(tài)頁(yè)面接收路由中的參數(shù)動(dòng)態(tài)渲染

可以通過(guò)watch監(jiān)聽(tīng)this.$router中的params的變化,也可以通過(guò)路由守衛(wèi)beforeRouteUpdate接收路由數(shù)據(jù),但是這里遇到了一個(gè)問(wèn)題,就是第一次跳轉(zhuǎn)的時(shí)候,沒(méi)有的watch和beforeRouteUpdate里面,目前不知道為啥

// 動(dòng)態(tài)路由指向的頁(yè)面,通過(guò)接受路由中的數(shù)據(jù)動(dòng)態(tài)渲染
<template>
  <div class="home">pages: {{ page }}</div>
</template>
<script lang="ts">
import { ref } from "vue";

export default {
  name: "dynamicHome",
  components: {},

  // problems: 第一次路由跳轉(zhuǎn),進(jìn)不到watch和路由守衛(wèi)里面

  // created() {
  //   this.$watch(
  //     () => this.$route.params,
  //     (toParams, previousParams) => {
  //       console.log(toParams, previousParams);
  //       this.page = toParams.id;
  //     }
  //   );
  // },
  // 路由導(dǎo)航守衛(wèi),
  beforeRouteUpdate(to, from) {
    console.log(to, from);
    this.page = to.params.id;
  },
  setup() {
    const page = "未定義";
    return {
      page,
    };
  },
  computed: {},
  methods: {},
};
</script>
<style scoped>
.home {
  color: #ffee05;
}
</style>

3.動(dòng)態(tài)路由跳轉(zhuǎn)操作頁(yè)面

// dymamicRouter.vue
<!-- 動(dòng)態(tài)路由跳轉(zhuǎn) -->
<template>
  <div class="single-router">
    <h3>動(dòng)態(tài)路由跳轉(zhuǎn)</h3>
    <button @click="gotoAbout('about')">go to About</button>
    <button @click="gotoAbout('home')">go to Home</button>
    <div>
      <router-link to="/dynamicPage/:id=home">首頁(yè)</router-link> |
      <router-link to="/dynamicPage/:id=about">關(guān)于</router-link>
    </div>
    <h4>路由內(nèi)容</h4>
    <router-view />
    <h4>結(jié)束</h4>
  </div>
</template>
<script>
export default {
  name: "singleRouter",
  components: {},
  setup() {},
  methods: {
    gotoAbout(val) {
      const router = this.$router;
      router.push(`/dynamicPage/:id=${val}`);
      return {};
    },
  },
};
</script>
<style scoped>
.single-router {
  background-color: #cccccc;
}
</style>

App.vue主頁(yè)面

主頁(yè)面中使用組件

即main.ts創(chuàng)建的vue實(shí)例的頁(yè)面,是整個(gè)應(yīng)用的起始頁(yè)面

// App.vue
<template>
  <h3>單頁(yè)面路由</h3>
  <single-router></single-router>
  <h3>動(dòng)態(tài)路由</h3>
  <dynamic-router></dynamic-router>
</template>
<script lang="ts">
import { ref } from "vue";
import singleRouter from "./components/singleRouter.vue";
import dynamicRouter from "./components/dynamicRouter.vue";

export default {
  name: "App",
  components: {
    singleRouter,
    dynamicRouter,
  },
  setup() {},
};
</script>
<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

遇到的一些問(wèn)題

1.router-link, router-view未注冊(cè)

這是因?yàn)樵趍ain.ts里面沒(méi)有正確注冊(cè)router插件

2.找不到路由定義的路徑

因?yàn)閞outer/index.js里面import的組件路徑不正確

3.控制臺(tái)報(bào)錯(cuò)

[Vue warn]: Component provided template option but runtime compilation is not supported in this build of Vue. Configure your bundler to alias “vue” to “vue/dist/vue.esm-bundler.js”

這是因?yàn)榻M件沒(méi)有編譯,vue-cli創(chuàng)建的項(xiàng)目在vue.config.js文件里加上一個(gè)配置屬性

  const { defineConfig } = require("@vue/cli-service");
module.exports = defineConfig({
  ...// 其他配置
  runtimeCompiler: true,  // 運(yùn)行時(shí)編譯
});

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue中使用ElementUI使用第三方圖標(biāo)庫(kù)iconfont的示例

    Vue中使用ElementUI使用第三方圖標(biāo)庫(kù)iconfont的示例

    這篇文章主要介紹了Vue中使用ElementUI使用第三方圖標(biāo)庫(kù)iconfont的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10
  • 關(guān)于el-tooltip使用\n換行的情況顯示

    關(guān)于el-tooltip使用\n換行的情況顯示

    這篇文章主要介紹了關(guān)于el-tooltip使用\n換行的情況顯示,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • 探秘Vue異步更新機(jī)制中nextTick的原理與實(shí)現(xiàn)

    探秘Vue異步更新機(jī)制中nextTick的原理與實(shí)現(xiàn)

    nextTick?是?Vue?提供的一個(gè)重要工具,它的作用主要體現(xiàn)在幫助我們更好地處理異步操作,下面就跟隨小編一起來(lái)探索一下nextTick的原理與實(shí)現(xiàn)吧
    2024-02-02
  • Element Popover 彈出框的使用示例

    Element Popover 彈出框的使用示例

    這篇文章主要介紹了Element Popover 彈出框的使用示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • vue 權(quán)限管理幾種實(shí)現(xiàn)方法

    vue 權(quán)限管理幾種實(shí)現(xiàn)方法

    本文主要介紹了vue 權(quán)限管理幾種實(shí)現(xiàn)方法,權(quán)限需要前后端結(jié)合,前端盡可能的去控制,更多的需要后臺(tái)判斷,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-02-02
  • Vue中的v-cloak使用解讀

    Vue中的v-cloak使用解讀

    本篇文章主要介紹了Vue中的v-cloak使用解讀,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-03-03
  • vue+highCharts實(shí)現(xiàn)可選范圍的圖表

    vue+highCharts實(shí)現(xiàn)可選范圍的圖表

    這篇文章主要為大家詳細(xì)介紹了vue+highCharts實(shí)現(xiàn)可選范圍的圖表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Vue實(shí)現(xiàn)表格中對(duì)數(shù)據(jù)進(jìn)行轉(zhuǎn)換、處理的方法

    Vue實(shí)現(xiàn)表格中對(duì)數(shù)據(jù)進(jìn)行轉(zhuǎn)換、處理的方法

    這篇文章主要介紹了Vue實(shí)現(xiàn)表格中對(duì)數(shù)據(jù)進(jìn)行轉(zhuǎn)換、處理的方法,需要的朋友可以參考下
    2018-09-09
  • Vue利用插件實(shí)現(xiàn)打印功能的示例詳解

    Vue利用插件實(shí)現(xiàn)打印功能的示例詳解

    這篇文章主要為大家詳細(xì)介紹了Vue如何利用vue-print-nb插件實(shí)現(xiàn)打印功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)一下
    2023-03-03
  • 基于vue2實(shí)現(xiàn)一個(gè)日歷組件

    基于vue2實(shí)現(xiàn)一個(gè)日歷組件

    最近在做一個(gè)類(lèi)似課程表的需求,需要自制一個(gè)日歷來(lái)支持功能及展現(xiàn),就順便研究一下應(yīng)該怎么開(kāi)發(fā)日歷組件,下面這篇文章主要給大家介紹了關(guān)于如何基于vue2實(shí)現(xiàn)一個(gè)日歷組件的相關(guān)資料,需要的朋友可以參考下
    2022-12-12

最新評(píng)論