通过10个实例小练习,快速入门熟练 Vue3.0 核心新特性(上)

简介: 通过10个实例小练习,快速入门熟练 Vue3.0 核心新特性

环境搭建


$ git pull https://github.com/vuejs/vue-next.git
$ cd vue-next && yarn


下载完成之后打开代码, 开启 sourceMap :


  • tsconfig.json 把 sourceMap 字段修改为 true: "sourceMap": true
  • rollup.config.js 在 rollup.config.js 中,手动键入: output.sourcemap = true
  • 生成 vue 全局的文件:yarn dev
  • 在根目录创建一个 demo 目录用于存放示例代码,并在 demo 目录下创建 html 文件,引入构建后的 vue 文件



api 的使用都是很简单的,下文的内容,看例子代码就能懂了的,所以下面的例子不会做过多解释。


reactive

reactive: 创建响应式数据对象setup 函数是个新的入口函数,相当于 vue2.x 中 beforeCreate 和 created,在 beforeCreate 之后 created 之前执行。
<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>Hello Vue3.0</title>
    <style>
        body,
        #app {
            text-align: center;
            padding: 30px;
        }
    </style>
    <script src="../../packages/vue/dist/vue.global.js"></script>
</head>
<body>
    <h3>reactive</h3>
    <div id='app'></div>
</body>
<script>
    const { createApp, reactive } = Vue
    const App = {
        template: `
            <button @click='click'>reverse</button> 
            <div style="margin-top: 20px">{{ state.message }}</div>
        `,
        setup() {
            console.log('setup ');
            const state = reactive({
                message: 'Hello Vue3!!'
            })
            click = () => {
                state.message = state.message.split('').reverse().join('')
            }
            return {
                state,
                click
            }
        }
    }
    createApp(App).mount('#app')
</script>
</html>


微信图片_20220512183419.gif


ref & isRef

ref : 创建一个响应式的数据对象isRef : 检查值是否是 ref 的引用对象。

isRef : 检查值是否是 ref 的引用对象。


<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>Hello Vue3.0</title>
    <style>
        body,
        #app {
            text-align: center;
            padding: 30px;
        }
    </style>
    <script src="../../packages/vue/dist/vue.global.js"></script>
</head>
<body>
    <h3>ref & isRef</h3>
    <div id='app'></div>
</body>
<script>
    const { createApp, reactive, ref, isRef } = Vue
    const App = {
        template: `
            <button @click='click'>count++</button> 
            <div style="margin-top: 20px">{{ count }}</div>
        `,
        setup() {
            const count = ref(0);
            console.log("count.value:", count.value)  // 0
            count.value++
            console.log("count.value:", count.value)  // 1
            // 判断某值是否是响应式类型
            console.log('count is ref:', isRef(count))
            click = () => {
                count.value++;
                console.log("click count.value:", count.value) 
            }
            return {
                count,
                click,
            }
        }
    }
    createApp(App).mount('#app')
</script>
</html>


微信图片_20220512183549.gif


Template Refs


使用 Composition API 时,反应性引用和模板引用的概念是统一的。


为了获得对模板中元素或组件实例的引用,我们可以像往常一样声明 ref 并从 setup() 返回。


<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>Hello Vue3.0</title>
    <style>
        body,
        #app {
            text-align: center;
            padding: 30px;
        }
    </style>
    <script src="../../packages/vue/dist/vue.global.js"></script>
</head>
<body>
    <h3>Template Refs</h3>
    <div id='app'></div>
</body>
<script>
    const { createApp, reactive, ref, isRef, toRefs, onMounted, onBeforeUpdate } = Vue
    const App = {
        template: `
            <div v-for="(item, i) in list" :ref="el => { divs[i] = el }">
                {{ item }}
            </div>
        `,
        setup() {
            const list = reactive([1, 2, 3])
            const divs = ref([])
            // make sure to reset the refs before each update
            onBeforeUpdate(() => {
                divs.value = []
            })
            onMounted(() => {
                // the DOM element will be assigned to the ref after initial render
                console.log(divs.value) // [<div/>]
            })
            return {
                list,
                divs
            }
        }
    }
    createApp(App).mount('#app')
</script>
</html>


微信图片_20220512183839.gif



<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>Hello Vue3.0</title>
    <style>
        body,
        #app {
            text-align: center;
            padding: 30px;
        }
    </style>
    <script src="../../packages/vue/dist/vue.global.js"></script>
</head>
<body>
    <h3>Template Refs</h3>
    <div id='app'></div>
</body>
<script>
    const { createApp, reactive, ref, isRef, toRefs, onMounted, onBeforeUpdate } = Vue
    const App = {
        template: `
            <div v-for="(item, i) in list" :ref="el => { divs[i] = el }">
                {{ item }}
            </div>
        `,
        setup() {
            const list = reactive([1, 2, 3])
            const divs = ref([])
            // make sure to reset the refs before each update
            onBeforeUpdate(() => {
                divs.value = []
            })
            onMounted(() => {
                // the DOM element will be assigned to the ref after initial render
                console.log(divs.value) // [<div/>]
            })
            return {
                list,
                divs
            }
        }
    }
    createApp(App).mount('#app')
</script>
</html>


微信图片_20220512183910.png


toRefs


toRefs : 将响应式数据对象转换为单一响应式对象

将一个 reactive 代理对象打平,转换为 ref 代理对象,使得对象的属性可以直接在 template 上使用。


<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>Hello Vue3.0</title>
    <style>
        body,
        #app {
            text-align: center;
            padding: 30px;
        }
    </style>
    <script src="../../packages/vue/dist/vue.global.js"></script>
</head>
<body>
    <h3>toRefs</h3>
    <div id='app'></div>
</body>
<script>
    const { createApp, reactive, ref, isRef, toRefs } = Vue
    const App = {
        // template: `
        //     <button @click='click'>reverse</button> 
        //     <div style="margin-top: 20px">{{ state.message }}</div>
        // `,
        // setup() {
        //     const state = reactive({
        //         message: 'Hello Vue3.0!!'
        //     })
        //     click = () => {
        //         state.message = state.message.split('').reverse().join('')
        //         console.log('state.message: ', state.message)
        //     }
        //     return {
        //         state,
        //         click
        //     }
        // }
        template: `
            <button @click='click'>count++</button> 
            <div style="margin-top: 20px">{{ message }}</div>
        `,
        setup() {
            const state = reactive({
                message: 'Hello Vue3.0!!'
            })
            click = () => {
                state.message = state.message.split('').reverse().join('')
                console.log('state.message: ', state.message)
            }
            return {
                click,
                ...toRefs(state)
            }
        }
    }
    createApp(App).mount('#app')
</script>
</html>


微信图片_20220512183956.gif

相关文章
|
3月前
|
JavaScript 前端开发 开发者
Vue第1天:特性概览
Vue第1天:特性概览
|
3月前
|
缓存 JavaScript
Vue3 的模板语法:指令、插值语法和其他相关特性
Vue3 的模板语法:指令、插值语法和其他相关特性
159 4
Vue3 的模板语法:指令、插值语法和其他相关特性
|
3月前
|
JavaScript 算法 编译器
Vue3的新特性
Vue3的新特性
139 0
|
5天前
|
JavaScript 算法 API
Vue 3有哪些新特性
【8月更文挑战第16天】Vue 3有哪些新特性
29 1
|
12天前
|
JavaScript 前端开发 搜索推荐
揭秘 Vue 3 的 Teleport 特性,让你实现跨组件传输内容,使得开发变得更加得心应手!!
揭秘 Vue 3 的 Teleport 特性,让你实现跨组件传输内容,使得开发变得更加得心应手!!
|
2月前
|
缓存 JavaScript 前端开发
vue的一些特性
vue的一些特性
20 0
|
3月前
|
开发框架 JavaScript 算法
了解vue3的基本特性和底层原理
Vue3的底层原理涵盖了响应式系统的Proxy-based实现、组件的模板编译与渲染更新机制、组合式API带来的逻辑组织变革,以及其他关键特性的具体实现。这些原理共同构成了Vue3强大、高效、灵活的现代前端开发框架基础。
243 2
|
3月前
|
JavaScript 前端开发 编译器
Vue 3:新特性与改进技术详解
【4月更文挑战第25天】Vue 3 提升了编译和运行时性能,引入了Composition API实现灵活代码复用,新增Teleport用于任意位置渲染,支持Fragment多根节点及Suspense处理异步组件。同时,TypeScript支持增强,自定义指令和全局API也得到优化。Vue 3的新特性旨在提供更高效、灵活的开发体验,持续引领前端技术发展。
|
9月前
|
JavaScript 前端开发 编译器
你知道Vue 3.0中Treeshaking特性吗?
你知道Vue 3.0中Treeshaking特性吗?
174 0
|
3月前
|
JavaScript 前端开发 API
Vue3有哪些新特性
【4月更文挑战第15天】 Vue3带来了显著性能提升和开发体验优化:更快的渲染速度、更小的捆绑体积、改进的内存管理、增强的TypeScript支持、引入Composition API提升代码复用性,以及使用Proxy改进响应式数据处理。这些特性使Vue3成为更高效、灵活和可靠的框架,为开发者创造高性能应用提供了强大工具。
29 0