每天忙碌的日子,也不要忘记了好好生活 🏠

vuePress-theme-reco happylay 🐑    2020 - 2021
每天忙碌的日子,也不要忘记了好好生活 🏠 每天忙碌的日子,也不要忘记了好好生活 🏠

Choose mode

  • dark
  • auto
  • light
主页
分类
  • 手册
  • 前端
  • 后端
  • 工作
  • 相册
  • 文档
标签
时间轴
文档
  • 轨迹
个人空间
  • 哔哩哔哩
  • 编辑博客
工具集
  • 后端工具

    • 在线json解析
    • yml格式转换
    • websocket测试
    • 时间戳转换
    • cron表达式
    • linux程序包
    • 大小写转换
    • toml格式转换
  • 后端框架

    • Spring
    • GoFrame
  • 前端工具

    • Vant移动端组件库
    • Element桌面端组件库
    • uni-app移动端框架
    • uview移动端框架
    • colorui2.0文档
    • Figma
    • Codepen
    • Dribbble
    • Iconfont阿里矢量图库
    • IconPark图标库
    • Icomoon
    • Remixicon
    • favicon图标制作
  • 开发环境

    • windows包管理器-baulk
    • windows包管理器-scoop
    • windows原版镜像
    • nexus3仓库
  • 微服务

    • 版本兼容关系
    • k8s在线配置
    • k8s接口文档
GitHub
author-avatar

happylay 🐑

34

文章

24

标签

主页
分类
  • 手册
  • 前端
  • 后端
  • 工作
  • 相册
  • 文档
标签
时间轴
文档
  • 轨迹
个人空间
  • 哔哩哔哩
  • 编辑博客
工具集
  • 后端工具

    • 在线json解析
    • yml格式转换
    • websocket测试
    • 时间戳转换
    • cron表达式
    • linux程序包
    • 大小写转换
    • toml格式转换
  • 后端框架

    • Spring
    • GoFrame
  • 前端工具

    • Vant移动端组件库
    • Element桌面端组件库
    • uni-app移动端框架
    • uview移动端框架
    • colorui2.0文档
    • Figma
    • Codepen
    • Dribbble
    • Iconfont阿里矢量图库
    • IconPark图标库
    • Icomoon
    • Remixicon
    • favicon图标制作
  • 开发环境

    • windows包管理器-baulk
    • windows包管理器-scoop
    • windows原版镜像
    • nexus3仓库
  • 微服务

    • 版本兼容关系
    • k8s在线配置
    • k8s接口文档
GitHub
  • 2020

    • api 接口服务
    • dom渲染总结
    • eslint代码规范
    • promise 异步总结
    • scss总结
    • typescript 总结
    • vue2 常用配置
    • vue2总结
    • vue3总结
    • vuex总结
    • 响应式布局
    • 基础样式
    • 常用js写法
    • 常用npm类库
    • 常用样式
    • 样式卡片

vue3总结

vuePress-theme-reco happylay 🐑    2020 - 2021

vue3总结

happylay 🐑 2020-12-31 代码片段vue3

摘要: vue3 总结 时间: 2020-12-23


# 模板

<template>
  <div></div>
</template>

<script lang="ts">
import { defineComponent, ref, toRefs, reactive, computed } from 'vue'
export default defineComponent({
  name: 'happylay',
  props: [],
  components: {},
  setup(props, context) {
    // 常用写法:setup(props, { attrs, slots, emit }) {
    console.log(props, context)
    function emitEvent() {
      context.emit('event', '注册事件')
    }
    const data = ref('基本类型数据,对象和数组会自动转为reactive代理对象')
    const obj = reactive({
      id: 1,
      name: '对象',
      msg: '递归深度响应式'
    })
    const computedValue = computed(() => {
      return '计算属性' + data.value
    })
    const computedMethods = computed({
      get() {
        return
      },
      set(val) {
        console.log('计算属性', val)
        return
      }
    })
    const getValue = () => {
      console.log('获取数据(用.value获取属性值)', data.value)
    }
    return {
      data,
      obj,
      ...toRefs(obj), // toRefs把响应式对象转换成普通对象,普通对象的每个属性都为ref
      emitEvent,
      getValue,
      computedValue,
      computedMethods
    }
  },
  beforeCreate() {
    console.log('数据初始化的生命周期回调')
  },
  mounted() {
    console.log(this)
  }
})
</script>

<style scoped lang="scss">
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

# ref其余用法

<template>
  <div>
    <h2>获取页面元素</h2>
    <input type="text" ref="inputRef" />
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted } from 'vue'
export default defineComponent({
  setup() {
    console.log('初始化')

    // 默认为空,页面加载完毕,组件存在后,获取文本框元素
    const inputRef = ref<HTMLElement | null>(null)

    // 页面加载后
    onMounted(() => {
      inputRef.value && inputRef.value.focus()
    })

    return {
      inputRef
    }
  }
})
</script>

<style scoped lang="scss">
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
编辑文档
最后一次更新: 2021/2/2 下午12:34:17
欢迎来到小屋。
看板娘