真正掌握vuex的使用方法(五)

希望每一位同学可以亲自将每行代码进行一次尝试!记得对于学习代码来讲慢慢来才会更快!

f

现在咱们先抛开vuex,一起来做一个如上图一样的切换案例:

<template>
    <div id="app">
        <!--点击此按钮index变为0,并让此按钮应用上样式active-->
       <input type="button" value="vue" :class="{active:index==0}" @click="index=0">
        <!--点击此按钮index变为1,并让此按钮应用上样式active-->
       <input type="button" value="es6" :class="{active:index==1}" @click="index=1">
        <!--点击此按钮index变为2,并让此按钮应用上样式active-->
       <input type="button" value="node" :class="{active:index==2}" @click="index=2">
        <!--当index为0时显示此DIV-->
        <div v-show="index==0">
            <p><a href="https://zhangpeiyue.com/archives/101">利用Vue-cli中的proxyTable解决开发环境的跨域问题</a></p>
            <p><a href="https://zhangpeiyue.com/archives/120">真正掌握vuex的使用方法(一)</a></p>
            <p><a href="https://zhangpeiyue.com/archives/122">真正掌握vuex的使用方法(二)</a></p>
            <p><a href="https://zhangpeiyue.com/archives/126">真正掌握vuex的使用方法(三)</a></p>
            <p><a href="https://zhangpeiyue.com/archives/127">真正掌握vuex的使用方法(四)</a></p>
        </div>
        <!--当index为1时显示此DIV-->
        <div v-show="index==1">
            <p><a href="https://zhangpeiyue.com/archives/92">es6箭头函数的理解及面试题</a></p>
            <p><a href="https://zhangpeiyue.com/archives/88">es6中class类的全方面理解(三)——静态方法</a></p>
            <p><a href="https://zhangpeiyue.com/archives/86">es6中class类的全方面理解(二)——继承</a></p>
            <p><a href="https://zhangpeiyue.com/archives/83">es6中class类的全方面理解(一)</a></p>
            <p><a href="https://zhangpeiyue.com/archives/72">es6中的模块化</a></p>
        </div>
        <!--当index为2时显示此DIV-->
        <div v-show="index==2">
            <p><a href="https://zhangpeiyue.com/archives/118">如何通过node.js对数据进行MD5加密</a></p>
        </div>
    </div>
</template>
<script>
    export default {
        name: 'App',
        data(){
            return {
                //index用于记录当前所选按钮的位置,值会根据点击按钮的不同而变化
                index:0
            }
        }
    }
</script>
<style>
    #app input,#app p{
        margin:5px;
        padding:5px;
    }
    #app input.active{
        background:red;
    }
    #app div{
        border:1px solid red;
    }
</style>

上面的代码并不复杂,相信大家也都可以看的明白。通过以上代码咱们可以实现一个简单的切换,通过这种形式来完成的切换可以称其为乞丐版的切换。因为里面的数据都被写死了!所以咱们可以将代码进行一些优化,将数据单独存放起来进行管理:
首先将所有的数据放到data中:

data(){
    return {
        tagList:[
            {
                tagName:"vue",//按钮的名字
                newList:[//vue新闻的内容列表
                    {
                        newTitle:"利用Vue-cli中的proxyTable解决开发环境的跨域问题",
                        newHref:"https://zhangpeiyue.com/archives/101"
                    },
                    {
                        newTitle:"真正掌握vuex的使用方法(一)",
                        newHref:"https://zhangpeiyue.com/archives/120"
                    },
                    {
                        newTitle:"真正掌握vuex的使用方法(二)",
                        newHref:"https://zhangpeiyue.com/archives/122"
                    },
                    {
                        newTitle:"真正掌握vuex的使用方法(三)",
                        newHref:"https://zhangpeiyue.com/archives/126"
                    },
                    {
                        newTitle:"真正掌握vuex的使用方法(四)",
                        newHref:"https://zhangpeiyue.com/archives/127"
                    }
                ]
            },
            {
                tagName:"es6",//按钮的名字
                newList:[//es6新闻的内容列表
                    {
                        newTitle:"es6箭头函数的理解及面试题",
                        newHref:"https://zhangpeiyue.com/archives/92"
                    },
                    {
                        newTitle:"es6中class类的全方面理解(三)——静态方法",
                        newHref:"https://zhangpeiyue.com/archives/88"
                    },
                    {
                        newTitle:"es6中class类的全方面理解(二)——继承",
                        newHref:"https://zhangpeiyue.com/archives/86"
                    },
                    {
                        newTitle:"es6中class类的全方面理解(一)",
                        newHref:"https://zhangpeiyue.com/archives/83"
                    },
                    {
                        newTitle:"es6中的模块化",
                        newHref:"https://zhangpeiyue.com/archives/72"
                    }
                ]
            },
            {
                tagName:"node",//按钮的名字
                newList:[//node新闻的内容列表
                    {
                        newTitle:"如何通过node.js对数据进行MD5加密",
                        newHref:"https://zhangpeiyue.com/archives/118"
                    }
                ]
            }
        ],
        //index用于记录当前所选按钮的位置,值会根据点击按钮的不同而变化
        index:0
    }
}

然后再将template进行修改

<div id="app">
    <!--对按钮进行遍历-->
   <input type="button" v-for="(item,i) in tagList" :value="item.tagName" :class="{active:i==index}" @click="index=i">
    <!--对新闻进行遍历-->
    <div v-for="(item,i) in tagList" v-show="i==index">
        <p v-for="info in item.newList"><a :href="info.newHref">{{info.newTitle}}</a></p>
    </div>
</div>

最后运行项目,如你所愿,正常运行,一切都是那么美好!
就目前来讲这些数据都已经被单独存放起来了,不过做到这样还远远不够,因为不管是按钮的文字还是新闻的内容正常来讲都是来自于后台人员提供的接口。所以咱们还要继续调整!
首先创建一个server.js,创建一个基于express的node服务来提供接口:

var express=require("express");
var app=express();
app.get("/getTagList",function(req,res){
    res.json([
        {
            tagName:"vue",
            newList:[
                {
                    newTitle:"利用Vue-cli中的proxyTable解决开发环境的跨域问题",
                    newHref:"https://zhangpeiyue.com/archives/101"
                },
                {
                    newTitle:"真正掌握vuex的使用方法(一)",
                    newHref:"https://zhangpeiyue.com/archives/120"
                },
                {
                    newTitle:"真正掌握vuex的使用方法(二)",
                    newHref:"https://zhangpeiyue.com/archives/122"
                },
                {
                    newTitle:"真正掌握vuex的使用方法(三)",
                    newHref:"https://zhangpeiyue.com/archives/126"
                },
                {
                    newTitle:"真正掌握vuex的使用方法(四)",
                    newHref:"https://zhangpeiyue.com/archives/127"
                }
            ]
        },
        {
            tagName:"es6",
            newList:[
                {
                    newTitle:"es6箭头函数的理解及面试题",
                    newHref:"https://zhangpeiyue.com/archives/92"
                },
                {
                    newTitle:"es6中class类的全方面理解(三)——静态方法",
                    newHref:"https://zhangpeiyue.com/archives/88"
                },
                {
                    newTitle:"es6中class类的全方面理解(二)——继承",
                    newHref:"https://zhangpeiyue.com/archives/86"
                },
                {
                    newTitle:"es6中class类的全方面理解(一)",
                    newHref:"https://zhangpeiyue.com/archives/83"
                },
                {
                    newTitle:"es6中的模块化",
                    newHref:"https://zhangpeiyue.com/archives/72"
                }
            ]
        },
        {
            tagName:"node",
            newList:[
                {
                    newTitle:"如何通过node.js对数据进行MD5加密",
                    newHref:"https://zhangpeiyue.com/archives/118"
                }
            ]
        }
    ]);
});
app.listen(80,function(){
    console.log("开启服务成功");
})

node server.js将服务开启。
通过设置proxyTable来解决开发环境中的跨域问题:

proxyTable: {
    "/api":{
        target:"http://127.0.0.1",//访问的服务器地址
        changeOrigin:true,//true为开启代理
        pathRewrite:{
            '^/api': ''//路径的替换规则
        }
    }
},

在App.vue中引入axios:

import axios from "axios";

在mounted周期函数中,通过axios来调用接口,改变tagList的值:

mounted(){
    axios.get("api/getTagList")
        .then(data=>{
            this.tagList=data.data;
        })
}

最后App.vue的内容:

<template>
<div id="app">
    <!--对按钮进行遍历-->
   <input type="button" v-for="(item,i) in tagList" :value="item.tagName" :class="{active:i==index}" @click="index=i">
    <!--对新闻进行遍历-->
    <div v-for="(item,i) in tagList" v-show="i==index">
        <p v-for="info in item.newList"><a :href="info.newHref">{{info.newTitle}}</a></p>
    </div>
</div>
</template>
<script>
    import axios from "axios";
    export default {
        name: 'App',
        data(){
            return {
                tagList:[],
                //index用于记录当前所选按钮的位置,值会根据点击按钮的不同而变化
                index:0
            }
        },
        mounted(){
            axios.get("api/getTagList")
                .then(data=>{
                    this.tagList=data.data;
                })
        }
    }
</script>
<style>
    #app input,#app p{
        margin:5px;
        padding:5px;
    }
    #app input.active{
        background:red;
    }
    #app div{
        border:1px solid red;
    }
</style>

一切都是那么的自然!
如果对node不是特别了解的同学,可以在static文件夹内创建一个名字叫tagList的json文件,用于存储数据值。

[
    {
        "tagName": "vue",
        "newList": [
            {
                "newTitle": "利用Vue-cli中的proxyTable解决开发环境的跨域问题",
                "newHref": "https://zhangpeiyue.com/archives/101"
            },
            {
                "newTitle": "真正掌握vuex的使用方法(一)",
                "newHref": "https://zhangpeiyue.com/archives/120"
            },
            {
                "newTitle": "真正掌握vuex的使用方法(二)",
                "newHref": "https://zhangpeiyue.com/archives/122"
            },
            {
                "newHref": "https://zhangpeiyue.com/archives/126"
            },
            {
                "newTitle": "真正掌握vuex的使用方法(四)",
                "newHref": "https://zhangpeiyue.com/archives/127"
            }
        ]
    },
    {
        "tagName": "es6",
        "newList": [
            {
                "newTitle": "es6箭头函数的理解及面试题",
                "newHref": "https://zhangpeiyue.com/archives/92"
            },
            {
                "newTitle": "es6中class类的全方面理解(三)——静态方法",
                "newHref": "https://zhangpeiyue.com/archives/88"
            },
            {
                "newTitle": "es6中class类的全方面理解(二)——继承",
                "newHref": "https://zhangpeiyue.com/archives/86"
            },
            {
                "newTitle": "es6中class类的全方面理解(一)",
                "newHref": "https://zhangpeiyue.com/archives/83"
            },
            {
                "newTitle": "es6中的模块化",
                "newHref": "https://zhangpeiyue.com/archives/72"
            }
        ]},
    {
        "tagName":"node",
        "newList":[
            {
                "newTitle":"如何通过node.js对数据进行MD5加密",
                "newHref":"https://zhangpeiyue.com/archives/118"
            }
        ]
    }
]

然后将mounted函数中调取的接口地址调整为"/static/tagList.json"即可:

mounted(){
    axios.get("/static/tagList.json")
        .then(data=>{
            this.tagList=data.data;
        })
}

今天就到这里吧,在下篇文章当中将会针对于这个案例咱们采用vuex来实现!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,706评论 4 366
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,002评论 1 301
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 110,462评论 0 250
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,375评论 0 216
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,763评论 3 294
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,849评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,033评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,768评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,490评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,734评论 2 253
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,204评论 1 264
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,566评论 3 260
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,227评论 3 241
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,137评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,934评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,926评论 2 283
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,774评论 2 274