Vue-全集

把一个json对象的数据,显示到元素上去
用js和jQuery是通过操作DOM来实现,Vue是直接将数据绑定上去

入门

<!--JS写-->
  <body>
        <div id="div1">
            
        </div>
        <script>
            var green={"hp":"123","name":"zhangsan"}
            
            var g=document.getElementById("div1");
            
            g.innerHTML = green.name
        </script>
    </body>

<!--Vue写-->
  <body>
        <div id="div1">
            {{g.name}}
        </div>
        <script src="http://how2j.cn/study/vue.min.js"></script>
        <script>
            var green = {"hp":"123","name":"zhangsan"}
            new Vue({
                    el:"#div1",
                    data:{
                        g:green
                    }
            })
        </script>
    </body>

事件

<!--单击事件-->
  <body>
        <div id="div1">
            <div>点击{{click1}}次</div>
            <button v-on:click="count">点击</button>
        </div>
        <script src="http://how2j.cn/study/vue.min.js"></script>
        <script>
            
            new Vue({
                    el:"#div1",
                    data:{
                        click1:0
                    },
                    methods:{
                        count:function(){
                            this.click1++;
                        }
                    }
            })
        </script>
    </body>

<!--冒泡-->
  <body>
        <div id="div1">
            <div id="father" v-on:click="doc">
                father
                <div id="me" v-on:click="doc">
                    me
                    <div id="son" v-on:click="doc">
                        son
                    </div>
                </div>
            </div>
        </div>
        <script src="http://how2j.cn/study/vue.min.js"></script>
        <script>
            
            new Vue({
                    el:"#div1",
                    data:{
                        id:''
                    },
                    methods:{
                        doc:function(){
                            //冒泡
                            this.id = event.currentTarget.id;
                            alert(this.id)
                        }
                    }
            })
        </script>
    </body>
冒泡停止:v-on:click.stop="doc"
优先触发:v-on:click.capture="doc"
只有自己触发:v-on:click.self="doc"
只能提交一次:v-on:click.once="doc"

条件

<!--v-if-->

  <body>
        <div id="div1">
            <button v-on:click="count">单击</button>
            <div v-if="show">默认看见</div>
        </div>
        <script src="http://how2j.cn/study/vue.min.js"></script>
        <script>
            
            new Vue({
                    el:"#div1",
                    data:{
                        show:true
                    },
                    methods:{
                        count:function(){
                            this.show = !this.show
                        }
                    }
            })
        </script>
    </body>

<!--v-else-->

  <body>
        <div id="div1">
            <button v-on:click="count">点击</button>
            <div v-if="show">中了</div>
            <div v-else>没中</div>
        </div>
        <script src="http://how2j.cn/study/vue.min.js"></script>
        <script>
            
            new Vue({
                    el:"#div1",
                    data:{
                        show:false
                    },
                    methods:{
                        count:function(){
                            this.show = Math.random()<0.1
                        }
                    }
            })
        </script>
    </body>

<!--v-else-if-->

  <body>
        <div id="div1">
            <button v-on:click="count">点击</button>
            <div v-if="show>90">神仙</div>
            <div v-else-if="show>85">国家领导人</div>
            <div v-else>乞丐</div>
        </div>
        <script src="http://how2j.cn/study/vue.min.js"></script>
        <script>
            
            new Vue({
                    el:"#div1",
                    data:{
                        show:0
                    },
                    methods:{
                        count:function(){
                            this.show = Math.random()*100
                        }
                    }
            })
        </script>
    </body>

<!--v-for-->

<body>
        <div id="div1">
            <table>
                <tr v-for="g,index in greens">
                    <td>{{index+1}}</td>
                    <td>{{g.name}}</td>
                    <td>{{g.age}}</td>
                </tr>
            </table>
        </div>
        <script src="http://how2j.cn/study/vue.min.js"></script>
        <script>
            
           var  green = [
                {name:"张三",age:123},
                {name:"张三",age:123},
                {name:"张三",age:123},
                {name:"张三",age:123},
                {name:"张三",age:123},
           ];
           
           new Vue({
             el:"#div1",
             data:{
                    greens:green
             }
           })
           
        </script>
    </body>

属性绑定

<!-- v-bind-->

  <body>
        <div id="div1">
            <a v-bind:href="page">
                点击
            </a>
        </div>
        <script src="http://how2j.cn/study/vue.min.js"></script>
        <script>
           new Vue({
             el:"#div1",
             data:{
                    page:"https://www.how2j.cn"
             }
           })
           
        </script>
    </body>

双向绑定

<!--v-model-->

  <body>
        <div id="div1">
            <input type="text" v-model="page"/>
            <button v-on:click="p">输出</button>
        </div>
        <script src="http://how2j.cn/study/vue.min.js"></script>
        <script>
           new Vue({
             el:"#div1",
             data:{
                    page:"pages"
             },
             methods:{
                    p:function(){
                        alert(this.page)
                    }
             }
           })
           
        </script>
    </body>

属性计算

<!--属性计算 computed-->
  <body>
        <div id="div1">
            汇率:<input type="text" v-model.number="exchange"/>
            人民币:<input type="text" v-model.number="money"/>
            美元:<td>{{doller}}</td>
        </div>
        <script src="http://how2j.cn/study/vue.min.js"></script>
        <script>
           new Vue({
             el:"#div1",
             data:{
                    exchange:6.4,
                    money:10
             },
             computed:{
                    doller:function(){
                        return this.money / this.exchange
                    }
             }
           })
           
        </script>
    </body>

<!--属性监听 watch -->
 <body>
        <div id="div1">
            汇率:<input type="text" v-model.number="exchange"/>
            人民币:<input type="text" v-model.number="money"/>
            美元:<input type="text" v-model.number="doller"/>
        </div>
        <script src="http://how2j.cn/study/vue.min.js"></script>
        <script>
           new Vue({
             el:"#div1",
             data:{
                    exchange:6.4,
                    money:0,
                    doller:0
             },
             watch:{
                    money:function(val){
                        this.money = val,
                        this.doller= this.money / this.exchange
                    }
             }
           })
           
        </script>
    </body>

过滤器

<!--filter 首字母大写和尾字母大写-->

  <body>
        <div id="div1">
            输入:<input type="text" v-model="exchange"/>
            输出:<td>
                    {{exchange|a|b}}
            </td>
        </div>
        <script src="http://how2j.cn/study/vue.min.js"></script>
        <script>
           Vue.filter('a',function(value){
                        if(!value) return ''
                        value=value.toString()
                        return value.charAt(0).toUpperCase() + value.substring(1)
           })
           
           Vue.filter('b',function(value){
                        if(!value) return ''
                        value=value.toString()
                        return value.substring(0,value.length-1) + value.charAt(value.length-1).toUpperCase()
           })
           new Vue({
             el:"#div1",
             data:{
                    exchange:''
             }
           })
           
        </script>
    </body>

组件

<!--如效果所示,每个产品展示,就是一个模板。 只用做一个模板,然后照
着这个模板,传递不同的参数就可以看到不同的产品展示了。
这个模板,就是组件。
Vue.component
props
template
-->

  <body>
        <div id="div1">
            <c v-for="p in products" v-bind:c="p"></c>
        </div>
        <script src="http://how2j.cn/study/vue.min.js"></script>
        <script>
         //全局组件
         Vue.component('c',{
            //参数
             props:['c'],
             template:'<div v-on:click="d">{{c.name}} 销量:{{c.sale}}</div>',
             //自定义事件
             methods:{
                d:function(){
                    this.c.sale++
                }
             }
         })
         
          new Vue({
             el:"#div1",
             data:{
                //json数据
                products:[
                    {"name":"众生","sale":"18"},
                    {"name":"众生","sale":"23"},
                    {"name":"众生","sale":"34"}
                ]
             }
          })
           
        </script>
    </body>

路由

<!--路由相当于局部刷新-->
  <body>
        <div id="div1">
            <div>
                <!--
            router-link 相当于就是超链
            to 相当于就是 href
        -->
                <router-link to="/c">用户管理</router-link>
                <router-link to="/d">产品管理</router-link>
                <router-link to="/e">订单管理</router-link>
            </div>
            <div>
                <!--
            点击上面的/c,那么/c 对应的内容就会显示在router-view 这里
        -->
                <router-view></router-view>
            </div>
        </div>
        <script src="http://how2j.cn/study/vue.min.js"></script>
        <script src="http://how2j.cn/study/vue-router.min.js"></script>
        <script>
          //1、申明三个模板
          var c={template:'<p>用户管理</p>'};
          var d={template:'<p>产品管理</p>'};
          var e={template:'<p>订单管理</p>'};
          //2、定义路由,将模板和超链接绑定
          var routes = [
            {path:'/',redirect:'/c'},//表示默认
            {path:'/c',component:c},
            {path:'/d',component:d},
            {path:'/e',component:e}
          ];
          //3、创建VueRouter实例
        var router=new VueRouter({
            routes:routes
        })
        //4、绑定路由
          new Vue({
             el:"#div1",
             router
          })
           
        </script>
    </body>

Axios.js

<!--axios.get(url).then(function(response){}-->

 <body>
        <div id="div1">
                <table>
                    <tr v-for="he in heros">
                        <td>{{he.name}}</td>
                        <td>{{he.hp}}</td>
                    </tr>
                </table>
        </div>
        <script src="http://how2j.cn/study/vue.min.js"></script>
        <script src="https://how2j.cn/study/axios.min.js"></script>
        <script>
            var url = "https://static.how2j.cn/study/jsons.txt"
          new Vue({
            el:'#div1',
            data:{
                heros:[]
            },
            mounted:function(){
                //this表示heros[]
                var h=this
                axios.get(url).then(function(response){
                    h.heros=response.data;//json数据放入,是字符串形式
                    h.heros=eval("("+h.heros+")");//json数据转数组
                })
            }
          })
        </script>
    </body>

Vue-cli

一款vue脚手架,需要node.js和webpack基础
安装:
1、切换目录:E:\system\Vue\vue-cli
2、命令:npm install -g @vue/cli@3.0.1
3、版本:vue --version
4、安装原型工具:npm install -g @vue/cli-service-global@3.0.1
进行测试:
a、在目录下E:\system\Vue\vue-cli,新建test.vue
内容
<template>
  <h1>Hello Vue-Cli</h1>
</template>
b、打开控制台,切换目录,运行命令:vue serve test.vue
c、浏览器打开链接:http://localhost:8080/
图片.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。