【星海出品】VUE(五)

2023-11-06 04:52

本文主要是介绍【星海出品】VUE(五),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

表单

表单输入绑定
只需要v-model声明一下这个变量就可以。
还可以选择不同的类型,例如 type="checkbox“
v-model 也提供了 lazy、number、.trim 功能,只需要在v-model后面加入.lazy
例如:v-model.lazy=”message“

<template><h1>表单</h1><form><input type="text" v-model="message"><p> {{ message }}</p></form><input type="checkbox" id="checkbox" v-model="checked"/><label for="checkbox"> {{ checked }} </label>
</template><script>
export default {data(){return{message:"",checked:false}}
}
</script>

ref用法
获取DOM节点

<div ref="container" class="container"> {{ content }} </div>
<button @click="getElementHandle"> 获取元素 </button>
<input type="texr" ref="username">
<button @click="getUserName">获取数据</button>
methods:{getElementHandle(){// innerHTML是原生的JS的DOM功能。this.$refs获取了DOM的信息。this.$refs.container.innerHTML = "哈哈";}
}
getUserName(){console.log(this.$refs.username.value);
}

引入步骤

1.引入组件
script中

import MyComponent from "./components/MyComponent.vue"

2.注入组件

export	default {components:{MyComponent}
}  

3.显示组件
<template>下

<MyComponent />

可选项是script和style

<template>
<!-- 写模板 -->
</template>
<script>
//写逻辑
</script>
<style>
/*写样式 */
</style>

App.vue

<script>
import MyComponents from "./components/MyComponents.vue"
export	default {components:{MyComponents}
}  
</script>
<template><MyComponents />
</template>
<style>
</style>

MyComponents.vue

<script>
export default{data(){return{message:"组件基础组成"}}
}
</script><template><div class="container"> {{ message }} </div>
</template><style>.container{font-size: 30px;color: red;
}</style>

scoped

<style scoped>
/* 一旦加了scoped就只在当前组件中生效 */
</style>

组件的嵌套

App.vue #下面包含Main.vue 和 Header.vue

<script>
import Header from "./pages/Header.vue"
import Main from "./pages/Main.vue"
import MyComponents from "./components/MyComponents.vue"
import Aside from "./pages/Aside.vue"
export	default {components:{// MyComponents,Header,Main,Aside}
}  
</script><template><!-- <MyComponents /> --><Header /><Main /><Aside />
</template><style></style>

main.vue #下面还嵌套了 Article.vue

<template><div class="main"><h3>Main</h3><Article /><Article /></div>
</template>
<script>
import Article from "./Article.vue"
export default{components:{Article}
}
</script>
<style scoped>
.main{float: left;width: 70%;height: 600px;border: 5px solid #999;box-sizing: border-box;/* border-top: 0px; */
}
</style>

Article

<template><h3>Article</h3>
</template>
<style scoped>
h3{width: 80%;margin: 0 auto;text-align: center;line-height: 100px;box-sizing: border-box;margin-top: 50px;background: #999;
}
</style>

Aside

<template><div class="aside"><h3> Aside </h3><Item /><Item /><Item /></div>
</template><script>
import Item from "./Item.vue"
export default {components:{Item}
}
</script><style scoped>
.aside{float: right;width: 30%;height: 600px;border: 5px solid #999;box-sizing: border-box;border-left: 0;border-top: 1;
}
</style>

Item.vue

<template><h3>Item</h3>
</template>
<style scoped>
h3{width: 80%;margin: 0 auto;text-align: center;line-height: 100px;box-sizing: border-box;margin-top: 10px;background: #999;
}
</style>

Header.vue

<template><h3>Header</h3>
</template>
<style scoped>
h3{width: 100%;height: 100px;border: 5px solid #999;text-align: center;line-height: 100px;box-sizing: border-box;
}
</style>

组件注册

全局注册 - 局部注册

main.js中去注册组件
这样就不用每个组件都去import 和 export default {components: }
全局注册,注册过,即使没有用到,也会打包到JS中,而且不利于后期维护

createApp(App).mount('#app')
// const app = createApp(App)
// 在这中间写组件的注册
// app.mount('#app')

局部注册,上面讲的三步就是局部注册。

组件的数据传递 _props

组件与组件之间不是完全独立的,而是有交集的,那就是组件与组件之间可以传递数据的。
传递数据的解决方案就是。props

父组件数据可以传递给子组件,数量没有限制。
如果向传递动态的数据,需要变成b-bind的形式进行传递
三级导入

App.vue

<script>
import Parent from "./components/Parent.vue"
export	default {components:{Parent}
}  
</script><template><Parent />
</template><style></style>

Parent.vue

<template><h3>Parent</h3><Child title="Parent数据" />
</template>
<script>
import Child from "./Child.vue"
export default{data(){return{}},components:{Child}
}
</script>

Child.vue

<template><h3>Child</h3><p> {{ title }} </p>
</template>
<script>
export default{data(){return{}},props:["title"]
}
</script>

props:[“title”]
#这个title要以字符串的形式存在

注意:只能从父集传到子集,但不能反向。
如果向传递数值类型,用v-bind形式

:age="age"
data(){ //data中声明
age:0
//子组件使用props进行接收后就可以使用
props:["title","age"]

数组类型传递

使用v-bind传递
使用 props:["names"] 接收子组件直接使用 <li v-for="(item,index) of names" :key="index"> {{ item }} </li> 使用

传递类型校验

props:{title:{type: String}
}

验证接收的数据是否是规定的类型,如果错误则不予接收。

type 可以验证多个类型
加函数default() 是工厂模式

props:{title:{type: [String,Number,Array,Object],required: true //加这个选项则这个为必选项},age:{type: Number,default:0},UserInfo:{type:object,default(){return {}}}names:{type:Array,default(){return ["空"] //不给传就返回一个 "空"  ,如果是数组或者默认值,要这么写}}
}

注意:子组件的函数,不允许修改父组件传过来的数据。
props是只读的。

组件事件

可以使用 $emit 方法触发自定义事件。
触发自定义事件的目的是组件之间的传递数据。

子传父使用自定义事件。

App.vue

<script>
import ComponentsEvent from "./components/ComponentsEvent.vue"
export	default {components:{ComponentsEvent}
}
</script><template><ComponentsEvent />
</template><style>
</style>

ComponentsEvent.vue

<template><h3>A事件</h3><Child @some-event="getHandle" /><p> A接收的数据:{{ message }} </p>
</template>
<script>
import Child from "./Child.vue"
export default {data(){return{message:""}},components:{Child},methods:{getHandle(data){this.message = data;}}
}
</script>

Child.vue

<template><h3>Child</h3><button @click="sendHandle">传递数据</button>
</template>
<script>
export default{methods:{sendHandle(){this.$emit("someEvent","B的数据")}}
}
</script>

小技巧
通过子给父传递参数,子使用watch监听器监听数据,然后通过

this.$emit("searchEvent",newValue)
//第一个参数,是父的函数的映射key。newvalue是返回的值

反馈回父

这篇关于【星海出品】VUE(五)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/354574

相关文章

前端如何通过nginx访问本地端口

《前端如何通过nginx访问本地端口》:本文主要介绍前端如何通过nginx访问本地端口的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、nginx安装1、下载(1)下载地址(2)系统选择(3)版本选择2、安装部署(1)解压(2)配置文件修改(3)启动(4)

HTML中meta标签的常见使用案例(示例详解)

《HTML中meta标签的常见使用案例(示例详解)》HTMLmeta标签用于提供文档元数据,涵盖字符编码、SEO优化、社交媒体集成、移动设备适配、浏览器控制及安全隐私设置,优化页面显示与搜索引擎索引... 目录html中meta标签的常见使用案例一、基础功能二、搜索引擎优化(seo)三、社交媒体集成四、移动

HTML input 标签示例详解

《HTMLinput标签示例详解》input标签主要用于接收用户的输入,随type属性值的不同,变换其具体功能,本文通过实例图文并茂的形式给大家介绍HTMLinput标签,感兴趣的朋友一... 目录通用属性输入框单行文本输入框 text密码输入框 password数字输入框 number电子邮件输入编程框

HTML img标签和超链接标签详细介绍

《HTMLimg标签和超链接标签详细介绍》:本文主要介绍了HTML中img标签的使用,包括src属性(指定图片路径)、相对/绝对路径区别、alt替代文本、title提示、宽高控制及边框设置等,详细内容请阅读本文,希望能对你有所帮助... 目录img 标签src 属性alt 属性title 属性width/h

CSS3打造的现代交互式登录界面详细实现过程

《CSS3打造的现代交互式登录界面详细实现过程》本文介绍CSS3和jQuery在登录界面设计中的应用,涵盖动画、选择器、自定义字体及盒模型技术,提升界面美观与交互性,同时优化性能和可访问性,感兴趣的朋... 目录1. css3用户登录界面设计概述1.1 用户界面设计的重要性1.2 CSS3的新特性与优势1.

HTML5 中的<button>标签用法和特征

《HTML5中的<button>标签用法和特征》在HTML5中,button标签用于定义一个可点击的按钮,它是创建交互式网页的重要元素之一,本文将深入解析HTML5中的button标签,详细介绍其属... 目录引言<button> 标签的基本用法<button> 标签的属性typevaluedisabled

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

CSS place-items: center解析与用法详解

《CSSplace-items:center解析与用法详解》place-items:center;是一个强大的CSS简写属性,用于同时控制网格(Grid)和弹性盒(Flexbox)... place-items: center; 是一个强大的 css 简写属性,用于同时控制 网格(Grid) 和 弹性盒(F

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求