Vue3-js 学习笔记

目录

前言

优秀的教程

reactive 数据绑定

<template>
<div class="app">
<h1>{{ state.name }}</h1>
</div>
</template> <script>
import { reactive } from "vue";
export default {
name: "App",
setup() {
const state = reactive({
name: "朱大常"
}); return { state };
}
};
</script>

事件绑定

<template>
<div class="app">
<h1>{{ state.name }}</h1>
<button type="button" @click="login">按钮</button>
</div>
</template> <script>
import { reactive } from "vue";
export default {
name: "App",
setup() {
// 数据定义
const state = reactive({
name: "朱大常"
});
// 事件定义
const login = () => {
state.name = "朱大常001";
};
return { state, login };
}
};
</script>

生命函数周期

onMounted

<template>
<div class="app"></div>
</template> <script>
import { onMounted } from "vue";
export default {
name: "App",
setup() {
// 组件加载
onMounted(() => {
console.log("组件已挂载");
});
}
};
</script>

计算属性-computed

computed (只要监听的那个数据改变,就会重新计算)

<template>
<div class="app">
<h1>{{ state.name }}</h1>
<h2>岁数:{{ state.age }}</h2>
<h3>计算后的属性:{{ state.setComputed }}</h3> <input type="text" v-model="state.username" />
<button type="button" @click="login">按钮</button>
</div>
</template> <script>
import { reactive, computed } from "vue";
export default {
name: "App",
setup() {
// 数据定义
const state = reactive({
name: "朱大常",
username: "",
age: 18,
setComputed: computed(() => state.age + 2)
});
// 事件定义
const login = () => {
state.name = state.username;
};
setTimeout(() => {
state.age = 19;
}, 2000);
return { state, login };
}
};
</script>

props

  • 备注:适用于同步传值,不是异步的,不是双向绑定的
<!-- 子组件 -->
<template>
<h1>子组件:{{ title }}</h1>
</template> <script>
import { onMounted } from "vue";
export default {
name: "test",
props: {
title: {
type: String,
default: ""
}
},
setup(props) {
onMounted(() => {
console.log("----组件加载完成-props值为", props.title);
});
}
};
</script> <!-- 父组件 -->
<template>
<div class="app">
<!-- 出现警告信息是 :title和 自己 定义的一样 -->
<test :title="state.title"></test>
</div>
</template> <script>
import { reactive } from "vue";
import test from "./components/test";
export default {
name: "App",
components: {
test
},
setup(props) {
// 数据定义
const state = reactive({
title: "朱大常"
});
return { state };
}
};
</script>

emit-自定义事件

emit 传值-注意这里的事件生命使用小写加-

<!-- 子组件 -->
<template>
<button type="button" @click="handleClick">emit按钮</button>
</template> <script>
import { onMounted } from "vue";
export default {
name: "test",
setup({ emit }) {
const handleClick = () => {
emit("my-event", "hello");
};
return { handleClick };
}
};
</script> <!-- 父组件 -->
<template>
<div class="app">
<test @my-event="parentClick"></test>
</div>
</template> <script>
import { reactive } from "vue";
import test from "./components/test";
export default {
name: "App",
components: {
test
},
setup(props) {
// 数据定义
const state = reactive({
name: "朱大常"
}); const parentClick = (e) => {
console.log(e);
}; return { parentClick };
}
};
</script>

ref-获取元素及子组件

ref 注意-相比 vue2.x 有很大的改变--ref 和 reactive 差别-ref 线

<!-- 子组件 -->
<template>
<div></div>
</template> <script>
import { ref } from "vue";
export default {
name: "test",
setup() {
// 1. 定义响应式的数据
const count = ref(1); // 2. 定义方法
const refsgh = () => {
console.log("-----k");
};
return { count, refsgh };
}
};
</script> <!-- 父组件 -->
<template>
<div class="app">
<test ref="comRef"></test>
<button @click="clickClid001">获取子组成数据</button>
<button @click="clickClid002">调用子组件方法</button>
</div>
</template> <script>
import { ref } from "vue";
import test from "./components/test";
export default {
name: "App",
components: {
test
},
setup() {
// 1. 创建一个组件的 ref 引用 一定要 return ️!!!!!!!!!!!!!!
const comRef = ref(null); // 2. 获取子组成的值
const clickClid001 = () => {
console.log(comRef.value.count);
}; // 3. 调用子组件的方法
const clickClid002 = () => {
comRef.value.refsgh();
};
// comRef 必须在return 抛出
return { comRef, clickClid001, clickClid002 };
}
};
</script>

watch

<template>
<div class="app">
<button @click="watch001">watch方法</button>
<!-- 这里.value可以省略 -->
<h4>ref值:{{ nameref }}</h4>
</div>
</template> <script>
import { watch } from "vue";
export default {
name: "App",
setup() {
const nameref = ref("朱大常");
watch(nameref, (newValue, oldValue) => {
// 输出
console.log(newValue, oldValue);
}); const watch001 = () => {
nameref.value = "张德帅";
};
return {
watch001,
nameref
};
}
};
</script>

vue3-组件通信

父组件获取子组件数据和调用子组件方法

  • props-破软丝 (传值)
  • ref-瑞府 (调用子组件的方法)
<!-- 子组件 -->
<template>
<h1>{{ title }}</h1>
</template> <script>
import { ref } from "vue";
export default {
name: "menus",
props: {
title: {
type: String,
default: "menus"
}
},
setup() {
// 1. 定义响应式的数据
const count = ref(1); // 2. 定义方法
const clidFun = () => {
console.log("-----k");
};
return { count, clidFun };
}
};
</script> <!-- 父组件 -->
<template>
<div class="app">
<menus ref="menusRef" :title="state.title"></menus>
<button @click="clickClid001">ref获取子组件数据</button>
<button @click="clickClid002">ref调用子组件方法</button>
</div>
</template> <script>
import { ref, reactive } from "vue";
import menus from "./components/menus";
export default {
name: "App",
components: {
menus
},
setup() {
const state = reactive({
title: "张德帅"
});
// 1. 创建一个组件的 ref 引用
const menusRef = ref(null); // 2. 获取子组成的值
const clickClid001 = () => {
console.log(menusRef.value.count);
}; // 3. 调用子组件的方法
const clickClid002 = () => {
menusRef.value.clidFun();
}; return { state, menusRef, clickClid001, clickClid002 };
}
};
</script>

子组件通信父组件

  • props
  • emit
<!-- 子组件 -->
<template>
<h1>{{ title }}</h1>
<button type="button" @click="getPropsState">props获取父组件数据</button
><br />
<button type="button" @click="getEmitState">emit调用父组件方法</button>
<br />
<button type="button" @click="parentClik">$parent调用父组件方法</button>
</template> <script>
export default {
name: "test001",
props: {
title: {
type: String,
default: ""
},
appFun: {
type: Function,
default: () => {}
}
},
setup(props, { emit }) {
// 1.props获取父组件数据
const getPropsState = () => {
props.appFun();
}; // 2.emit调用父组件方法
const getEmitState = () => {
emit("my-even");
}; return { getPropsState, getEmitState };
}
};
</script> <style lang="scss" scoped></style> <!-- 父组件 -->
<template>
<div>
<h1>子组件</h1>
<test01 :title="state.title" :appFun="appFun" @my-even="clidFun"></test01>
</div>
</template> <script>
import { reactive } from "vue";
import test01 from "./components/test001";
export default {
name: "App",
components: {
test01
},
setup() {
const state = reactive({
title: "子组件"
}); // props 方法
const appFun = () => {
console.log("--------P");
}; // emit 自定义事件
const clidFun = () => {
console.log("父组件方法被调用");
}; return { state, appFun, clidFun };
}
};
</script> <style lang="scss" scoped></style>

v-slot-插槽

<!-- 子组件 -->
<template>
<div class="HeadNav-main">
<!-- 默认插槽 -->
<slot>
<!-- slot内为后备内容 -->
<h3>没传内容</h3>
</slot> <!--具名插槽-->
<slot name="header"> </slot> <!--作用域插槽-->
<slot name="footer"> </slot>
</div>
</template> <!-- 父组件 -->
<template>
<child>
<!--默认插槽-->
<template v-slot>
<div>默认插槽</div>
</template>
<!--具名插槽-->
<template #header>
<div>具名插槽</div>
</template>
<!--作用域插槽-->
<template #footer="slotProps">
<div>
{{slotProps.testProps}}
</div>
</template>
<child>
</template>

reactive-ref-区别

reactive-ref-取值区别proxy

const state = reactive({
name:"asd",
formDataInput:{
name:"asd",
age:12
}
})
const parm = Object.assign({}, state.formDataInput);
const a1 = { ...state.formDataInput };

watchEffect-监听值变化

  • 值发生变化,执行这个函数
const state = ref();
watchEffect(() => {
state.value = props.details;
});

vue3 js 学习笔记的更多相关文章

  1. js学习笔记:webpack基础入门(一)

    之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...

  2. Vue.js学习笔记(2)vue-router

    vue中vue-router的使用:

  3. JS 学习笔记--9---变量-作用域-内存相关

    JS 中变量和其它语言中变量最大的区别就是,JS 是松散型语言,决定了它只是在某一个特定时间保存某一特定的值的一个名字而已.由于在定义变量的时候不需要显示规定必须保存某种类型的值,故变量的值以及保存的 ...

  4. WebGL three.js学习笔记 使用粒子系统模拟时空隧道(虫洞)

    WebGL three.js学习笔记 使用粒子系统模拟时空隧道 本例的运行结果如图: 时空隧道demo演示 Demo地址:https://nsytsqdtn.github.io/demo/sprite ...

  5. WebGL three.js学习笔记 法向量网格材质MeshNormalMaterial的介绍和创建360度全景天空盒的方法

    WebGL学习----Three.js学习笔记(5) 点击查看demo演示 Demo地址:https://nsytsqdtn.github.io/demo/360/360 简单网格材质 MeshNor ...

  6. WebGL three.js学习笔记 创建three.js代码的基本框架

    WebGL学习----Three.js学习笔记(1) webgl介绍 WebGL是一种3D绘图协议,它把JavaScript和OpenGL ES 2.0结合在一起,通过增加OpenGL ES 2.0的 ...

  7. vue.js 学习笔记3——TypeScript

    目录 vue.js 学习笔记3--TypeScript 工具 基础类型 数组 元组 枚举 字面量 接口 类类型 类类型要素 函数 函数参数 this对象和类型 重载 迭代器 Symbol.iterat ...

  8. 2019-4-29 js学习笔记

    js学习笔记一:js数据类型   1:基本数据类型       number类型(整数,小数)      String类型          boolean类型        NaN类型其实是一个nu ...

  9. 一点感悟:《Node.js学习笔记》star数突破1000+

    写作背景 笔者前年开始撰写的<Node.js学习笔记> github star 数突破了1000,算是个里程碑吧. 从第一次提交(2016.11.03)到现在,1年半过去了.突然有些感慨, ...

  10. JS学习笔记5_DOM

    1.DOM节点的常用属性(所有节点都支持) nodeType:元素1,属性2,文本3 nodeName:元素标签名的大写形式 nodeValue:元素节点为null,文本节点为文本内容,属性节点为属性 ...

随机推荐

  1. 三台服务器上离线安装redis哨兵集群,一主二从三哨兵

    三台服务器上离线安装redis哨兵集群,一主二从三哨兵 系统安装好gcc环境,(不然编译redis会报错).依旧不知道怎么离线安装gcc环境的可查看CentOS下离线安装gcc环境,图文详细 下载 点 ...

  2. OI之奇葩错误

    链式前向星(写法 \(1\)):\(head\) 数组不初始化成 \(-1\).(同学写题) 链式前向星(写法 \(2\)):\(idx\) 不初始化成 \(1\).(同学写题) 用到队列时忘记 \( ...

  3. 我们常用的地铁卡/银行卡,竟然运行着一个 Java 虚拟机

    我们日常使用 NFC 卡可以用来刷地铁,进出门禁,但是你有没有想过, 当我们使用一个 NFC 的 IC 卡刷卡进入地铁的时候,此时系统是如何知道我这个卡上有多少充值余额的? 这个薄薄的 NFC 卡到底 ...

  4. 牛客周赛 Round 6

    牛客周赛 Round 6 A-游游的数字圈_牛客周赛 Round 6 (nowcoder.com) 枚举即可 #include <bits/stdc++.h> #define int lo ...

  5. 兼容ios11的正则匹配

    ios11不支持正则零宽断言,以字符串 $哈哈哈(sh039488)$ 为例: 不兼容写法:/\$(?<=\$).*?(?=\)\$)\)\$/g 兼容写法:/\$(.*?)\)\$/g

  6. C# 使用HtmlAgilityPack 抓取 网站链接

    今天在找电视剧下载链接的时候,找了一个整部剧的下载地址,但是有40多集,链接地址较长且不好复制,于是就想到了HtmlAgilityPack抓取的方式. 先看实现效果: 使用到的NUGET包: Html ...

  7. 六,Spring Boot 容器中 Lombok 插件的详细使用,简化配置,提高开发效率

    六,Spring Boot 容器中 Lombok 插件的详细使用,简化配置,提高开发效率 @ 目录 六,Spring Boot 容器中 Lombok 插件的详细使用,简化配置,提高开发效率 1. Lo ...

  8. bacnet mstp设备数据 转IEC61850项目案例

    目录 1 案例说明 1 2 VFBOX网关工作原理 1 3 使用YABE软件读取BACNET MSTP设备信息 2 4 配置网关采集BACNET MSTP数据 4 5 用IEC61850协议转发数据 ...

  9. electron-builder打包配置说明

    发现问题 通过vue建立的项目使用electron-builder打包(不会eletron打包vue项目的看这里)出exe后发现名字就直接是项目文件夹的名字,但此时想自定义汉字名称,通过尝试直接修改p ...

  10. Ollama + JuiceFS:一次拉取,到处运行

    今天这篇博客转载自我们的全栈工程师朱唯唯.在使用 Ollma 进行大模型加载时,她尝试使用了 JuiceFS 进行模型共享,JuiceFS 的数据预热和分布式缓存功能显著提升了加载效率,优化了性能瓶颈 ...