当前位置: 首页 > news >正文

采用模版建网站的缺点太原网站建设制作

采用模版建网站的缺点,太原网站建设制作,沈阳网站外包公司,做足球原创短视频网站shallowRef和shallowReactive shallowRef 作用&#xff1a;创建一个响应式数据&#xff0c;但只对顶层属性进行响应式处理 用法 let myVar shallowRef(initialValue)特点&#xff1a;只跟踪引用值变化&#xff0c;不关心值内部的属性变化 案例 <template><div c…

shallowRef和shallowReactive

shallowRef

  1. 作用:创建一个响应式数据,但只对顶层属性进行响应式处理

  2. 用法

    let myVar = shallowRef(initialValue)
    
  3. 特点:只跟踪引用值变化,不关心值内部的属性变化

案例

<template><div class="app"><h2>求和为:{{ sum }}</h2><h2>名字为:{{ person.name }}</h2><h2>年龄为:{{ person.age }}</h2><h2>汽车为:{{ car }}</h2><button @click="changeSum">sum+1</button><button @click="changeName">修改名字</button><button @click="changeAge">修改年龄</button><button @click="changePerson">修改整个人</button><span>|||||</span><button @click="changeBrand">修改品牌</button><button @click="changeColor">修改颜色</button><button @click="changeEngine">修改发动机</button><!-- <button @click="changeCar">修改整个车</button> --></div>
</template><script setup lang="ts" name="App">import { ref,reactive,shallowRef,shallowReactive } from 'vue'let sum = shallowRef(0)let person = shallowRef({name: '小黑',age:2})let car = shallowReactive({brand: '特斯拉',options: {color: '黑色',engine:'v8'}})function changeSum (){sum.value += 1;}// 不会改变function changeName (){person.value.name = '大狗';}function changeAge (){person.value.age = 5;}//只能改变第一层function changePerson (){person.value = {name:'乐乐',age:3}}/* ********** */function changeBrand () {car.brand = '宝马'}function changeColor () {car.options.color = '红色'}function changeEngine () {car.options.engine = 'v12'}// function changeCar () {// 	Object.assign(car,{})// }
</script><style scoped>.app{background-color: #ddd;border-radius: 10px;box-shadow: 0 0 10px;padding: 10px;}button{margin:0 5px;}
</style>

总结

通过使用shallowRef()和shallowReactive()来绕开深度响应。浅层式API创建的状态只在其顶层是响应式的,对所有深层的对象不会做任何处理,避免了对每一个内部属性做响应式所带来的性能成本,这使得属性的访问控制变得更快、可提升性能。

readonly与shallowReadonly

readonly

  1. 作用:用于创建一个对象的深只读副本

  2. 用法

    <template><div class="app"><h2>当前求和为:{{ sum }}</h2><h2>当前sum2求和为:{{ sum2 }}</h2><h2>当前car1为:{{ car }}</h2><h2>当前car2为:{{ car2 }}</h2><button @click="changeSum">点我sum+1</button><button @click="changeSum2">点我sum2+1</button></div>
    </template><script setup lang="ts" name="App">import { ref, readonly, reactive,shallowReadonly } from 'vue';let sum = ref(0)let sum2 = readonly(sum)function changeSum() {sum.value += 1}function changeSum2() {sum2.value += 1  //sum2不可修改}
    </script><style scoped>.app{background-color: #ddd;border-radius: 10px;box-shadow: 0 0 10px;padding: 10px;}button{margin:0 5px;}
    </style>
    
  3. 特点:

    • 对象的所有嵌套属性变成只读
    • 任何尝试修改这个对象的操作都会被组织(在开发模式下,还会再控制台中发出警告)
  4. 应用场景:

    • 创建不可变的状态快照
    • 保护全局状态或配置不被修改

shallowReadonly

  1. 作用:与readonly类似,但只作用于对象的顶层属性,其他地方仍然可以修改

  2. 用法

    <template><div class="app"><h2>当前car1为:{{ car }}</h2><h2>当前car2为:{{ car2 }}</h2><button @click="changeBrand1">修改car1品牌</button><button @click="changeColor1">修改car1颜色</button><button @click="changePrice1">修改car1价格</button><button @click="changeBrand2">修改car2品牌</button><button @click="changeColor2">修改car2颜色</button><button @click="changePrice2">修改car2价格</button></div>
    </template><script setup lang="ts" name="App">import { reactive,shallowReadonly } from 'vue';let car = reactive({brand:'奔驰',options: {color:'黑色',price:100}})let car2 = shallowReadonly(car)function changeBrand1() {car.brand = '宝马'}function changeColor1() {car.options.color = '红色'}function changePrice1() {car.options.price += 10}function changeBrand2() {car2.brand = '宝马'}function changeColor2() {car2.options.color = '红色'}function changePrice2() {car2.options.price += 10}
    </script><style scoped>.app{background-color: #ddd;border-radius: 10px;box-shadow: 0 0 10px;padding: 10px;}button{margin:0 5px;}
    </style>
    

toRaw与markrow

toRaw

  1. 作用:用于获取一个响应式对象的原始对象,toRaw返回的对象不再是响应式,不会触发视图更新。

    • 何时使用

      在需要将响应式对象传递给非Vue的库或外部系统时,使用toRaw可以确保他们收到的是普通对象

  2. 用法:使用toRaw之后 年龄不再改变

    <template><div class="app"><h2>姓名:{{ person.name }}</h2><h2>年龄:{{ person2.age }}</h2><button @click="person.age += 1">修改年龄</button></div>
    </template><script setup lang="ts" name="App">
    import { reactive,toRaw } from 'vue';let person = reactive({name: '小黑',age:2
    })
    let person2 = toRaw(person)console.log('响应式数据', person)
    console.log('原始数据',person2)
    </script><style scoped>.app{background-color: #ddd;border-radius: 10px;box-shadow: 0 0 10px;padding: 10px;}button{margin:0 5px;}
    </style>
    

markRaw

  1. 作用:标记一个对象,使其永远都不会变成响应式

    • 何时使用

      使用第三方库的时候,防止错误把第三库对象变成响应式对象,就可以使用markRaw

  2. 用法

    <template><div class="app"><h2>汽车:{{ car2 }}</h2><button @click="car2.price += 10">修改价格</button></div>
    </template><script setup lang="ts" name="App">
    import { reactive,toRaw,markRaw } from 'vue';/* markRaw */
    //添加markRaw 28行就失去作用,无法改变成响应式
    let car = markRaw({ brand: '奔驰', price: 100 })
    let car2 = reactive(car)console.log(car);
    console.log(car2);</script><style scoped>.app{background-color: #ddd;border-radius: 10px;box-shadow: 0 0 10px;padding: 10px;}button{margin:0 5px;}
    </style>
    

customRef的使用

作用:创建一个自定义的ref,并对其依赖项跟踪和更新触发进行逻辑控制

实现防抖效果(useSumRef.ts)

<template><div class="app"><h2>{{ msg }}</h2><input type="text" v-model="msg"></div>
</template><script setup lang="ts" name="App">import { ref,customRef } from 'vue'//使用vue提供的默认ref定义响应式数据,数据一变,页面视图也更新// let msg = ref('你好')//使用vue提供的customRef定义响应式数据let initValue = '你好'let timer:number//track(跟踪)、trigger(触发) 不加上页面效果不会改变let msg = customRef((track,trigger) => {return {//get何时调用?——msg被读取时get() { track() //告诉vue数据很重要,需要对msg进行持续关注,一旦msg变化就去更新return initValue},//set何时调用?——msg被修改时set(value) {clearTimeout(timer)timer = setTimeout(() => {// console.log('set',value);initValue = valuetrigger() //通知vue数据变化了}, 1000);}}})
</script><style scoped>.app{background-color: #ddd;border-radius: 10px;box-shadow: 0 0 10px;padding: 10px;}button{margin:0 5px;}
</style>

采用hooks封装

定义hooks——useMsgRef.ts

import { customRef } from 'vue';export default function (initValue: string, delay: number) {//使用vue提供的customRef定义响应式数据// let initValue = '你好'let timer: number//track(跟踪)、trigger(触发) 不加上页面效果不会改变let msg = customRef((track, trigger) => {return {//get何时调用?——msg被读取时get() {track() //告诉vue数据很重要,需要对msg进行持续关注,一旦msg变化就去更新return initValue},//set何时调用?——msg被修改时set(value) {clearTimeout(timer)timer = setTimeout(() => {// console.log('set',value);initValue = valuetrigger() //通知vue数据变化了}, delay); //延迟时间不要写死}}})//hooks必须要有返回值return { msg }
}

App.vue

<template><div class="app"><h2>{{ msg }}</h2><input type="text" v-model="msg"></div>
</template><script setup lang="ts" name="App">import useMsgRef from './useMsgRef';//使用useMsgRef来定义一个响应式数据且有延迟效果let { msg } = useMsgRef('小黑', 2000)</script><style scoped>.app{background-color: #ddd;border-radius: 10px;box-shadow: 0 0 10px;padding: 10px;}button{margin:0 5px;}
</style>

Teleport

定义:Teleport是一种能够将我们的组件html结构移动到指定位置的技术

App.vue

<template><div class="outer"><h2>我是App组件</h2><img src="https://img.zcool.cn/community/014b275d0a1c95a801213ec257bb95.jpg@2o.jpg" alt=""><br><Modal/></div>
</template><script setup lang="ts" name="App">import Modal from './Modal.vue';
</script><style  scoped>
.outer{background-color: #ddd;border-radius: 10px;padding:5px;box-shadow: 0 0 10px;width: 500px;height: 500px;/* 添加滤镜后,模块定位不再参考视口,参考的是父容器了 */filter:saturate(150%);
}
img{height: 180px;object-fit: cover;
}
</style>

modal.vue

<template><button @click="isshow = true">展示弹窗</button><!-- teleport解决添加滤镜后fix定位问题 --><teleport to='body'><div class="modal" v-show="isshow"><h2>我是弹窗标题</h2><p>我是弹窗内容</p><button @click="isshow = false">关闭弹窗</button></div></teleport>
</template><script setup lang="ts" name="Modal">
import { ref } from 'vue'
let isshow = ref(false)
</script><style  scoped>
.modal{width: 200px;height: 150px;background-color: skyblue;border-radius: 10px;padding: 5px;box-shadow: 0 0 5px;text-align: center;position:fixed;left:50%;top: 20px;margin-left: -100px;
}
</style>

Suspense

  • 等待异步组件时渲染一些额外内容,让应用有更好的用户体验
  • 使用步骤:
    • 异步引入组件
    • 使用Suspense包裹组件,并配置好default和fallback

App.vue

<template><div class="app"><h2>我是App组件</h2><!-- 解决异步事件,如果不加下面,那么Child组件不显示 --><Suspense><template v-slot:default><Child/></template></Suspense></div>
</template><script setup lang="ts" name="App">import { Suspense } from 'vue'import Child from './Child.vue'
</script><style scoped>
.app{border-color: #ddd;border-radius:10px;padding:10px;box-shadow: 0 0 10px;
}
</style>

Child.vue

<template><div class="child"><h2>我是Child组件</h2><h3>当前sum求和为:{{ sum }}</h3></div>
</template><script setup lang="ts">
import { ref } from 'vue'
import axios from 'axios'let sum = ref(0)
let xxx = await axios.get('https://api.uomg.com/api/rand.qinghua?format=json')
console.log(xxx);
//如果要看到数据,可以解构 xxx 替换成 {data{content}} log直接打印content即可</script><style scoped>
.child{background-color: skyblue;border-radius:10px;padding:10px;box-shadow: 0 0 10px;
}
</style>

全局API转移到应用对象

  • app.component

App.vue

<template><div class="app"><h2>我是App组件</h2><Hello/><Child/></div>
</template><script setup lang="ts" name="App">import Child from './Child.vue'
</script><style scoped>
.app{border-color: #ddd;border-radius:10px;padding:10px;box-shadow: 0 0 10px;
}
</style>

Child.vue

<template><div class="child"><h2>我是Child组件</h2><h3>当前sum求和为:{{ sum }}</h3><Hello/></div>
</template><script setup lang="ts">
import { ref } from 'vue'let sum = ref(0)</script><style scoped>
.child{background-color: skyblue;border-radius:10px;padding:10px;box-shadow: 0 0 10px;
}
</style>

Hello.vue

<template><h2 style="color:red">你好</h2>
</template><script setup lang="ts" ></script><style scoped></style>

main.ts

import { createApp } from 'vue'
import App from './App.vue'
import Hello from './Hello.vue'// 创建应用
const app = createApp(App)app.component('Hello', Hello)// 挂载应用
app.mount('#app')
  • app.config

main.ts

app.config.globalProperties 添加 那么全局都可以使用

import { createApp } from 'vue'
import App from './App.vue'
import Hello from './Hello.vue'// 创建应用
const app = createApp(App)app.component('Hello', Hello)
app.config.globalProperties.x = 99//这样下面x就不会爆红
declare module 'vue' {interface ComponentCustomProperties {// $http: typeof axios// $translate: (key: string) => stringx: number}
}// 挂载应用
app.mount('#app')

比如:Child.vue 页面就会呈现 我是Child组件99 这里x会爆红 在main.ts添加定义部分

<template><div class="child"><h3>当前sum求和为:{{ sum }}</h3><Hello/></div>
</template><script setup lang="ts">
import { ref } from 'vue'let sum = ref(0)</script><style scoped>
.child{background-color: skyblue;border-radius:10px;padding:10px;box-shadow: 0 0 10px;
}
</style>
  • app.directive

main.ts

import { createApp } from 'vue'
import App from './App.vue'
import Hello from './Hello.vue'// 创建应用
const app = createApp(App)app.directive('beauty', (element, { value }) => {element.innerText += valueelement.style.color = 'green'element.style.backgroundColor = 'yellow'
})// 挂载应用
app.mount('#app')

比如:Child.vue 页面就会呈现 不高兴0——黄色背景,绿色文字

<template><div class="child"><h2>我是Child组件{{ x }}</h2><h3>当前sum求和为:{{ sum }}</h3><h4 v-beauty="sum">不高兴</h4><Hello/></div>
</template><script setup lang="ts">
import { ref } from 'vue'let sum = ref(0)</script><style scoped>
.child{background-color: skyblue;border-radius:10px;padding:10px;box-shadow: 0 0 10px;
}
</style>
  • app.mount

main.ts

import { createApp } from 'vue'
import App from './App.vue'
import Hello from './Hello.vue'
import type axios from 'axios'// 创建应用
const app = createApp(App)// 挂载应用
app.mount('#app')
  • app.unmount

main.ts 2秒钟之后 app消失在页面上

import { createApp } from 'vue'
import App from './App.vue'
import Hello from './Hello.vue'
import type axios from 'axios'// 创建应用
const app = createApp(App)// 挂载应用
app.mount('#app')setTimeout(() => {app.unmount()
}, 2000);
  • app.use

见router那一节 安装插件

//使用路由器
app.use(router)

Vue3的非兼容性改变

Vue2和Vue3区别

看vue官网:Breaking Changes | Vue 3 Migration Guide (vuejs.org)

  • 过渡类名v-enter 修改为 v-enter-from、过渡类名 v-leave 修改为 v-leave-from 。
  • keyCode 作为 v-on 修饰符的支持。
  • v-model 指令在组件上的使用已经被重新设计,替换掉了v-bind.sync。
  • v-if 和 v-for 在同一个元素身上使用时的优先级发生了变化。
  • 移除了 Son、 Soff 和 Sonce 实例方法。
  • 移除了过滤器 filter。
  • 移除了 $children 实例 propert。
http://www.hkea.cn/news/366188/

相关文章:

  • 福州论坛建站模板策划网络营销活动
  • 网站建设 天津百度市场应用官方app
  • 动态网站制作流程友情链接的定义
  • 企业网站开发方案免费建立一个网站
  • 网站引导页面制作的四个任务名称推广引流的10个渠道
  • 南宁网站建设制作后台网站关键词优化价格
  • 微信小程序商城制作公司宁波seo推广服务
  • 响应式购物网站公司seo是什么意思
  • 360未经证实的网站如何做电商运营方案
  • 网站建设类公司排名营销方案范文100例
  • 郑州网站设计 郑州网站开发网络优化有前途吗
  • 黑河做网站首页关键词排名优化
  • 网站二级域名怎么解析公司网络搭建
  • wordpress做网店win10优化大师是官方的吗
  • 弄个做网站公司产品宣传
  • 商品房建设信息网站googleplay商店
  • 菏泽 网站建设优化工具箱
  • 网站建设找哪家公司百度搜索热度
  • 网页设计论文引言北海百度seo
  • 网站空间哪家做的好网络营销的常用工具
  • 网站开发具体问题优化营商环境
  • wordpress4.5 火车头廊坊seo培训
  • 怎么做多个网站单点登录艺考培训
  • 网站怎么做双语种seo关键词如何设置
  • 用java做的游戏下载网站有哪些内容成都网络推广优化
  • 慈溪市网站建设google官网
  • 网站建设计划seo网站排名优化软件是什么
  • 大连网站建设谁家好郴州网站定制
  • 网站建设背景怎么写一个企业该如何进行网络营销
  • 为女朋友做的表白网站百度大数据分析工具