首页 > 其他 > 详细

03、Vue基础详解二

时间:2020-04-08 17:42:16      阅读:44      评论:0      收藏:0      [点我收藏+]

表单控件的绑定(v-model的双向绑定)

text类型

//视图模板
<div>
    <h3>text</h3>
    <input type="text" v-model="text">
    <p>{{text}}</p>
</div>
//data函数
data(){
    return{
        text:‘‘
    }
}

 

radio类型

//视图模板
<div>
    <h3>radio</h3>
    <input type="radio" name="sex" value="male" v-model="radio" id="1"> <label for="1">Male</label><br>
    <input type="radio" name="sex" value="female" v-model="radio" id="2"><label for="2">Female</label><br>
    <p>{{radio}}</p>
</div>
//data函数
data(){
    return{
        radio:‘‘
    }
}

 

checkbox类型

//视图模板
<div>
    <h3>checkbox</h3>
    <input type="checkbox" name="aihao" value="打篮球" v-model="checkbox">打篮球<br>
    <input type="checkbox" name="aihao" value="打" v-model="checkbox">打<br>
    <input type="checkbox" name="aihao" value="篮" v-model="checkbox">篮<br>
    <input type="checkbox" name="aihao" value="球" v-model="checkbox">球<br>
    <p>{{checkbox}}</p>
</div>
//data函数
data(){
    return{
        checkbox:[]
    }
}

 

select类型

//视图模板
<div>
    <h3>select</h3>
    <select v-model="select">
        <option disabled value="">请选择</option>
        <option>A</option>
        <option>B</option>
        <option>C</option>
    </select>
    <p>{{ select }}</p>
</div>
//data函数
data(){
    return{
        checkbox:[]
    }
}

 

表单提交

//视图模板
<button v-on:click="submit">表单提交按钮</button>
//methods函数
methods:{
    submit(){
        var shujv = {
        radio:this.radio,
        checkbox:this.checkbox,
        select:this.select
        };
    console.log(shujv);
    }
}

 

状态管理(vuex)

安装

进入项目目录进行安装(可换cnpm)

npm install vuex --save

 

在src -> main.js 引入

import store from ‘./store‘

 

并在new Vue代码段里,router下面使用store

router,
store,

 

在src目录下创建store文件夹

在store下创建index.js,并在里面写

import Vue from ‘vue‘
import Vuex from ‘vuex‘

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        count: 0,
        num: 1
    },
    mutations: {
        increment (state, num) {
          state.count++
          state.num = num;
        }
    },
    actions: {
        inc ({ commit }, obj) {
              commit(‘increment‘, obj)
        }
    }
})

 

获取数据

//获取 store下 -> index.js 里的全局数据

//任何页面都可以进行获取

//视图
<div>
    <div>{{bbc}}</div>
    <button @click="qq">获取vuex全局数据</button>
</div>
//data
data(){
    return{
        bbc:‘默认信息‘,
    }
}
//methods
methods:{
    qq(){
        this.bbc = this.$store.state.count;
        //获取store下,state对象,count属性的值
    }
}

 

修改数据

//methods
methods:{
    qq(){
        //修改数据
        this.$store.dispatch(‘inc‘,2342);
        //原理:
        //调用store下的inc函数,并把2342的参数传过去
        //通过inc修改state下的值
        alert(this.bbc);
    }
}

 

slot插槽

components下新建组件,plu.vue

<template>
  <div>
    <p>插槽顶部</p>
    <slot></slot>

    <p>插槽末尾</p>
    <slot name="mowei"></slot>
  </div>
</template>

 

正常调用插件

//调用页:正常引入
import sslot from ‘@/components/plu.vue‘

//调用页:正常注册
components:{
    sslot
}
//调用页:正常调用
<div>
    <sslot>
    </sslot>
</div>

 

按照slot的name及默认值插入

<sslot>
    <slot>
        //<!-- components组件页无论有多少slot标签,调用页只能有一个slot标签-->
        <span v-bind:style="{color:‘red‘}">这是在调用插件的页面写的,默认插入到没有name值得slot里面</span>
<span slot="mowei" v-bind:style="{color:‘orange‘}">这里slot="mowei",表示插入到name="mowei"的slot插槽里面</span> //<!-- ,里面内容以slot=‘xxx‘的形式,分类插入到组件页slot的name=‘xxx‘里面 --> </slot> </sslot>

 

vue-resource请求

//该插件提供了使用XMLHttpRequest或JSONP 进行Web请求和处理响应的服务。

安装

1、进入项目目录,开控制台下载(适当换成cnpm)

npm install vue-resource --save

 

2、src下main.js配置

//引入
import VueResource from ‘vue-resource‘
//全局使用
Vue.use(VueResource);

 

使用

//在生命周期函数mounted下,进行使用

GET

//get请求
this.$http.get(‘/xxurl.php‘).then(response => {     // /xxurl.php表示请求的url地址
    console.log(response.body);     //response.body表示返回的数据
}, response => {
    //错误回调
});

 

POST

//post请求
this.$http.post(‘/someUrl‘, {     // /xxurl.php表示请求的url地址
    foo: ‘bar‘        //这里是参数传递
}).then(response => {
    console.log(response.body);      //response.body表示返回的数据
}, response => {
    //错误回调
});

 

GET(带参数)

//get请求(带参数)
this.$http.get(‘/someUrl‘, {
    params: {     //params为发送的参数对象
        foo: ‘bar‘
    },
    headers: {     //设置请求头
        ‘X-Custom‘: ‘...‘
    }
}).then(response => {
    //成功回调
}, response => {
    //错误回调
});

 

03、Vue基础详解二

原文:https://www.cnblogs.com/mingliangge/p/12654165.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!