Vue组件和插槽使用


表单

针对一般元素,比如div,span 、p、 img等,采用的是单向绑定: v-bind 只要把数据绑定到视图中就可以,但是对于表单这种交互比较强的元素或组件,我们一般可能需求双向绑定,即:用户对视图元素的操作同时更新数据。

v-model 在内部为不同的输入元素使用不同的属性和事件来处理数据

  • text & textarea
  • checkbox & radio
  • select
text 和 textrea
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        
    </style>
    <!-- 通过script的方式引入vue -->
    <!-- 开发环境版本,包含了有帮助的命令行警告 -->
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>

<body>
    <div id="app">
        <input type="text" v-model="v1">
        <textarea name="" id="" v-model="v2" cols="30" rows="10"></textarea>
    </div>

    <script>
        let app = new Vue({
    
            //el指定id为app的元素
            el: "#app",
            //template替换的是id为app的元素的所有内容

            data: {
    
                v1:'aaa',
                v2:'bbbb'
            },
            
        })
    </script>
</body>

</html>

效果图如下


image.png

checkbox 和radio

checkbox 和 radio 使用checked属性和change事件

  • 单选框绑定一个值
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        
    </style>
    <!-- 通过script的方式引入vue -->
    <!-- 开发环境版本,包含了有帮助的命令行警告 -->
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>

<body>
    <div id="app">
        <input type="radio" name="" id="" v-model="v3"value="男">男
        <input type="radio" name="" id="" v-model="v3" value=""><button @click="getData">按钮</button>
    </div>

    <script>
        let app = new Vue({
    
            //el指定id为app的元素
            el: "#app",
            //template替换的是id为app的元素的所有内容

            data: {
    
                v1:'aaa',
                v2:'bbbb',
                v3:'男'
            },
            methods:{
    
                getData(){
    
                    console.log(this.v3);
                }
            }
            
        })
    </script>
</body>

</html>

效果如下
image.png

  • 多选框绑定到一个布尔值或者数组
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        
    </style>
    <!-- 通过script的方式引入vue -->
    <!-- 开发环境版本,包含了有帮助的命令行警告 -->
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>

<body>
    <div id="app">
        <input type="radio" name="" id="" v-model="v3"value="男">男
        <input type="radio" name="" id="" v-model="v3" value=""><button @click="getData">按钮</button>
        <!--  -->
        <hr>
        <!-- 单选 -->
        <input type="checkbox" v-model="v4">同意
        <hr>
        <!-- 多选 -->
        <input type="checkbox" name="" id="" v-model="v5" value="足球">足球
        <input type="checkbox" name="" id="" v-model="v5" value="篮球">篮球
    </div>

    <script>
        let app = new Vue({
    
            //el指定id为app的元素
            el: "#app",
            //template替换的是id为app的元素的所有内容

            data: {
    
                v1:'aaa',
                v2:'bbbb',
                v3:'男',
                v4:true,
                v5:['足球','音乐']
            },
            methods:{
    
                getData(){
    
                    console.log(this.v3);
                }
            }
            
        })
    </script>
</body>

</html>

效果如下
image.png

select

select字段将value 作为prop并将change作为事件

  • 单选绑定到值,多选绑定到数组
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        
    </style>
    <!-- 通过script的方式引入vue -->
    <!-- 开发环境版本,包含了有帮助的命令行警告 -->
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>

<body>
    <div id="app">
        <!-- 单选 -->
        <select name="" id="" v-model="v3">
            <option value=""></option>
            <option value=""></option>
        </select>
        <!-- 多选 -->
        <select name="" id="" v-model="v5" multiple>
            <option value="足球">足球</option>
            <option value="音乐">音乐</option>
        </select>
        <button @click="getData">按钮</button>
    </div>

    <script>
        let app = new Vue({
    
            //el指定id为app的元素
            el: "#app",
            //template替换的是id为app的元素的所有内容

            data: {
    
                v1:'aaa',
                v2:'bbbb',
                v3:'女',
                v4:true,
                v5:['足球','音乐']
            },
            methods:{
    
                getData(){
    
                    console.log(this.v3,this.v5);
                }
            }
            
        })
    </script>
</body>

</html>

效果如下
image.png

computed与watch

computed

在实际的应用中,我们会有一些原始数据,同时在应用中又会有一些数据是根据某些原始数据派生出来的一种情况,vue定义一个专门用来处理这种派生的数据的选项, computed

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当我们封装一个 Vue 组件时,可以使用插槽(slot)来让组件使用者可以在组件内部插入内容。使用插槽可以让我们的组件更加灵活和可复用。 在组件内部,我们可以使用 `slot` 标签来声明一个插槽。例如: ```html <template> <div class="my-component"> <h2>这是组件的标题</h2> <slot></slot> </div> </template> ``` 上面的代码,我们在组件的模板使用了 `slot` 标签,这个标签表示这个组件需要一个插槽使用这个组件的时候,我们可以在组件标签内部插入任意的内容,这些内容会被插入到 `slot` 标签的位置上。 例如,我们可以这样使用这个组件: ```html <template> <div> <MyComponent> <p>这里是插入到组件的内容</p> </MyComponent> </div> </template> ``` 这样,`<p>这里是插入到组件的内容</p>` 就会被插入到组件的模板的 `slot` 标签的位置上。 如果我们需要多个插槽,可以给 `slot` 标签添加一个 `name` 属性来区分不同的插槽。例如: ```html <template> <div class="my-component"> <h2>这是组件的标题</h2> <slot name="header"></slot> <div class="content"> <slot></slot> </div> <slot name="footer"></slot> </div> </template> ``` 上面的代码,我们声明了三个插槽,分别是名为 `header`、`default` 和 `footer` 的插槽使用这个组件时,我们可以这样插入内容: ```html <template> <div> <MyComponent> <template v-slot:header> <h3>这是头部插槽的内容</h3> </template> <p>这里是默认插槽的内容</p> <template v-slot:footer> <p>这是底部插槽的内容</p> </template> </MyComponent> </div> </template> ``` 上面的代码,我们使用了带 `v-slot` 的 `<template>` 标签来指定要插入哪个插槽,例如 `v-slot:header` 表示要插入到名为 `header` 的插槽。在 `<template>` 标签内部,我们可以插入任意的内容,这些内容会被插入到对应的插槽位置上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值