ITPub博客

首页 > IT职业 > IT职场 > 01-vue基础

01-vue基础

IT职场 作者:d0e1f 时间:2021-09-09 09:21:25 0 删除 编辑

双向数据绑定

指令

v-model

双向绑定

v-text

v-html

v-bind

可以绑定html标签的任何一个属性
示例:

    

v-if

绑定一个boolean值,如果为true输出,如果为false则不输出

v-show

绑定一个boolean值,如果为true显示,如果为false则不显示display:none

v-for

遍历

//第一种写法
        
  •         {{item}}             
//第二种写法 设置唯一标示
        
  •         {{index}} {{item}}             
//第三种写法 遍历对象   :key="item.id"
        
  •         {{index}} {{key}} {{item}}    

v-on

//注册事件//简写
//script中添加执行的方法
    export default {
        data() {            return {                msg: 'Hello Vue',                tip: '这是一个提示',                isOK: false
            }
        },        methods: {
            btnClick() {                this.isOK = !this.isOK;
            }
        }
    }

指令的两个缩写

v-on:click --> @click
v-bind:id  --> :id

组件

  • 子组件的基本使用

//注意:component中的data要返回functionVue.component('my-item', {
    data() {        return {            count: 0
        }
    },    template: '
  • {{count}}
  • '})var app = new Vue({    el: '#app',    data: {        msg: 'world'     } })
    • 控制组件的范围(父组件给子组件传值)

    var app = new Vue({
        el: '#app',
        data: {
            msg: 'hello'
        },
        components: {        'my-item': {
                props: ['test'],
                template: '

    {{test}}

    '         }     } });
    • 子组件通知父组件

    
    Vue.component('my-item', {
        data() {        return {}
        },
        props: ['count'],
        template: '
    count: {{count}}
    ',     methods: {         divClick() {             this.$emit('increate', '子组件传来的值');         }     } })var app = new Vue({     el: '#app',     data: {         count: 0     },     methods: {         increateDemo(c) {             console.log(c);         }     } });
    • 加载.vue的子组件(需要配置好webpack)

        import App from './07-vue.vue' //加载.vue组件
      
        new Vue({      el: '#app', //将组件中的内容插入到页面中指定的元素
            render: c => c(App) //编译app.vue组件
        })

    过滤器

    • 私有过滤器

               {{msg | toLower | replace('l','x')}}       
      
    • 全局过滤器

               {{msg | toLower | replace('l','n')}}       
      

    路由

        首页     登录          
           
      
    '     })    var login = Vue.component('login', {       template: '
    这是登录
    '     })    var router = new VueRouter({       routes: [         {name:'index', path: '/index', component: index},          {name:'login', path: '/login', component: login}       ]     })    var vm = new Vue({       el: '#app',       router: router     })
    • 获取路由参数

        首页     登录          
           
      

    发送ajax请求-vue-resource

    https://github.com/pagekit/vue-resource

    • 发送get请求

             
            
    • {{item.title}}
    •     
      
      
    • 发送post请求

                
      



    作者:琪先生_zZ
    链接:https://www.jianshu.com/p/f5ea69d409f1

    来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4019/viewspace-2802589/ ,如需转载,请注明出处,否则将追究法律责任。

    请登录后发表评论 登录
    全部评论

    注册时间:2019-10-17

    • 博文量
      75
    • 访问量
      552