Vue-CLI項目路由案例彙總

来源:https://www.cnblogs.com/pythonywy/archive/2019/09/01/11442932.html
-Advertisement-
Play Games

0901自我總結 Vue CLI項目路由案例彙總 router.js components/Nav.vue views/Course.vue components/CourseCard.vue vue <! 邏輯跳轉 {{ card.title }} <! 鏈接跳轉 <! 第一種 <! <route ...


0901自我總結

Vue-CLI項目路由案例彙總

router.js

import Vue from 'vue'
import Router from 'vue-router'
import Course from './views/Course'
import CourseDetail from './views/CourseDetail'

Vue.use(Router);

export default new Router({
    mode: 'history',
    base: process.env.BASE_URL,
    routes: [
        {
            path: '/course',
            name: 'course',
            component: Course,
        },
        {
            path: '/course/detail/:pk',  // 第一種路由傳參
            // path: '/course/detail',  // 第二、三種路由傳參
            name: 'course-detail',
            component: CourseDetail
        },
    ]
})

components/Nav.vue

<template>
    <div class="nav">
        <router-link to="/page-first">first</router-link>
        <router-link :to="{name: 'page-second'}">second</router-link>
        <router-link to="/course">課程</router-link>
    </div>
</template>

<script>
    export default {
        name: "Nav"
    }
</script>

<style scoped>
    .nav {
        height: 100px;
        background-color: rgba(0, 0, 0, 0.4);
    }
    .nav a {
        margin: 0 20px;
        font: normal 20px/100px '微軟雅黑';
    }
    .nav a:hover {
        color: red;
    }
</style>

views/Course.vue

<template>
    <div class="course">
        <Nav></Nav>
        <h1>課程主頁</h1>
        <CourseCard :card="card" v-for="card in card_list" :key="card.title"></CourseCard>
    </div>
</template>

<script>
    import Nav from '@/components/Nav'
    import CourseCard from '@/components/CourseCard'
    export default {
        name: "Course",
        data() {
            return {
                card_list: [],
            }
        },
        components: {
            Nav,
            CourseCard
        },
        created() {
            let cards = [
                {
                    id: 1,
                    bgColor: 'red',
                    title: 'Python基礎'
                },
                {
                    id: 3,
                    bgColor: 'blue',
                    title: 'Django入土'
                },
                {
                    id: 8,
                    bgColor: 'yellow',
                    title: 'MySQL刪庫高級'
                },
            ];
            this.card_list = cards;
        }
    }
</script>

<style scoped>
    h1 {
        text-align: center;
        background-color: brown;
    }
</style>

components/CourseCard.vue

<template>
    <div class="course-card">
        <div class="left" :style="{background: card.bgColor}"></div>
        <!-- 邏輯跳轉 -->
        <div class="right" @click="goto_detail">{{ card.title }}</div>
        
        <!-- 鏈接跳轉 -->
        <!-- 第一種 -->
        <!--<router-link :to="`/course/detail/${card.id}`" class="right">{{ card.title }}</router-link>-->
        <!-- 第二種 -->
        <!--<router-link :to="{-->
            <!--name: 'course-detail',-->
            <!--params: {pk: card.id},-->
        <!--}" class="right">{{ card.title }}</router-link>-->
        <!-- 第三種 -->
        <!--<router-link :to="{-->
            <!--name: 'course-detail',-->
            <!--query: {pk: card.id}-->
        <!--}" class="right">{{ card.title }}</router-link>-->
    </div>
</template>

<script>
    export default {
        name: "CourseCard",
        props: ['card'],
        methods: {
            goto_detail() {
                // 註:在跳轉之前可以完成其他一些相關的業務邏輯,再去跳轉
                let id = this.card.id;
                // 實現邏輯跳轉
                // 第一種
                this.$router.push(`/course/detail/${id}`);
                // 第二種
                // this.$router.push({
                //     'name': 'course-detail',
                //     params: {pk: id}
                // });
                // 第三種
                // this.$router.push({
                //     'name': 'course-detail',
                //     query: {pk: id}
                // });

                // 在當前頁面時,有前歷史記錄與後歷史記錄
                // go(-1)表示返回上一頁
                // go(2)表示去向下兩頁
                // this.$router.go(-1)
            }
        }
    }
</script>
<style scoped>
    .course-card {
        margin: 10px 0 10px;
    }
    .left, .right {
        float: left;
    }
    .course-card:after {
        content: '';
        display: block;
        clear: both;
    }
    .left {
        width: 50%;
        height: 120px;
        background-color: blue;
    }
    .right {
        width: 50%;
        height: 120px;
        background-color: tan;
        font: bold 30px/120px 'STSong';
        text-align: center;
        cursor: pointer;
        display: block;
    }
</style>

views/CourseDetail.vue

<template>
    <div class="course-detail">
        <h1>詳情頁</h1>
        <hr>
        <div class="detail">
            <div class="header" :style="{background: course_ctx.bgColor}"></div>
            <div class="body">
                <div class="left">{{ course_ctx.title }}</div>
                <div class="right">{{ course_ctx.ctx }}</div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        name: "CourseDetail",
        data() {
            return {
                course_ctx: '',
                val: '',
            }
        },
        created() {
            // 需求:獲取課程主頁傳遞過來的課程id,通過課程id拿到該課程的詳細信息
            // 這是模擬後臺的假數據 - 後期要換成從後臺請求真數據
            let detail_list = [
                {
                    id: 1,
                    bgColor: 'red',
                    title: 'Python基礎',
                    ctx: 'Python從入門到入土!'
                },
                {
                    id: 3,
                    bgColor: 'blue',
                    title: 'Django入土',
                    ctx: '扶我起來,我還能戰!'
                },
                {
                    id: 8,
                    bgColor: 'yellow',
                    title: 'MySQL刪庫高級',
                    ctx: '九九八十二種刪庫跑路姿勢!'
                },
            ];
            // let id = 1;
            // this.$route是專門管理路由數據的,下麵的方式是不管哪種傳參方式,都可以接收
            let id = this.$route.params.pk || this.$route.query.pk;
            for (let dic of detail_list) {
                if (dic.id == id) {
                    this.course_ctx = dic;
                    break;
                }
            }
        }
    }
</script>

<style scoped>
    h1 {
        text-align: center;
    }
    .detail {
        width: 80%;
        margin: 20px auto;
    }
    .header {
        height: 150px;
    }
    .body:after {
        content: '';
        display: block;
        clear: both;
    }
    .left, .right {
        float: left;
        width: 50%;
        font: bold 40px/150px 'Arial';
        text-align: center;
    }
    .left { background-color: aqua }
    .right { background-color: aquamarine }

    .edit {
        width: 80%;
        margin: 0 auto;
        text-align: center;

    }
    .edit input {
        width: 260px;
        height: 40px;
        font-size: 30px;
        vertical-align: top;
        margin-right: 20px;
    }
    .edit button {
        width: 80px;
        height: 46px;
        vertical-align: top;
    }
</style>

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • ### MVP簡介 >MVP 全稱:Model-View-Presenter ;MVP 是從經典的模式MVC演變而來,它們的基本思想有相通的[地方](https://baike.baidu.com/item/%E5%9C%B0%E6%96%B9/2262175):Controller/Present ...
  • indexOf,lastIndexOf,find,findIndex,every,some,forEach,map,filter,reduce,reduceRight ...
  • 首先放置一連的image 然後設置樣式,主要是圖片的大小 最後就是實現行為 一個簡單的小案例便完成了。效果圖如下: ps:div可以多給點,以防看不出效果 ...
  • Vue起飛前的準備 一、什麼是ECMAScript,以及es6的誕生? 1997年 ECMAScript 1.0 誕生 1999年12月 ECMAScript 3.0誕生,它 是一個巨大的成功,在業界得到了廣泛的支持,它奠定了JS的基本語法,被其後版本完全繼承。直到今天,我們一開始學習JS,其實就是 ...
  • 溫習一下js中的迭代方法。 <script type="text/javascript"> var arr = [1, 2, 3, 4, 5, 4, 3, 2, 1]; //所有項為false,則為false //否則直到遍歷到第一個為true的值,返回true //類似於數學里的‘或’ conso ...
  • 前言 上傳文件在開發中是很常見的操作,今天我選擇使用koa-multer中間件來實現這一功能,除了上傳文件外,我還會對文件上傳進行限制,以及發生上傳錯誤時的處理。 由於原來的 koa-multer 已經停止維護,我們要使用最新的 @koa/multer 。這個模塊是 koa-multer 的一個分支 ...
  • 網站是分為網站的前臺和網站的後臺. 前臺--給用戶看的 例如:商城 後臺--給管理員看的 例如:商城後臺 目的:用來添加維護數據 BootStrap:jsp 頁面顯示,效果好,美觀,適合作為用戶界面. EasyUI : jsp頁面,快速開發,格式統一,美觀效果一般. EasyUI裡面有很多組件(功能... ...
  • 一. "Vue的介紹及安裝和導入" 二. "Vue的使用" 三. "Vue成員獲取" 四. "Vue中的迴圈以及修改差值表達式" 五. "vue中methods,computed,filters,watch的總結" 六. "Vue中組件" 七. "Vue中插槽指令" 八. "Vue部分編譯不生效,解 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...