vue3新特性 | <style> 中使用js变量

9 篇文章 0 订阅
2 篇文章 0 订阅
1.1 问题描述
  • 想要把从父组件传来的颜色,大小等样式属性应用到css中
1.2 代码片段
<template>
  <p>hello vue3</p>
</template>
<script setup lang="ts">
import { ref, toRefs } from "vue";
interface PropType {
	color?:string,
	fontSize?:string | number
}
const prop = withDefaultProps(defineProps<PropType>(),{
	color:'red',
	fontSize:18
})
1.3 解决方案
  • 使用vue3新特性 css v-bind
<template>
  <p>hello vue3</p>
</template>
<script setup lang="ts">
import { ref, toRefs } from "vue";
interface PropType {
	color?:string,
	fontSize?:string | number
}
const prop = withDefaultProps(defineProps<PropType>(),{
	color:'red',
	fontSize:18
})
const { color, fontSize } = toRefs(prop)

if(typeof(fontSize?.value) === 'number'){
	fontSize?.value = fontSize?.value + 'px'
}
</script>
<style scoped>
p {
  color: v-bind(color);
  font-size: v-bind(fontSize)
}
</style>
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
条件语句 v-if ,v-else,v-else-if。 后两者必须放在v-if之后 循环语句 v-for 指令需要以 site in sites 形式的特殊语法, sites 是源数据数组并且 site 是数组元素迭代的别名。 Computed VS methods 我们可以使用 methods 来替代 computed,效果上两个都是一样的,但是 computed 是基于它的依赖缓存,只有相关依赖发生改变时才会重新取值。而使用 methods ,在重新渲染的时候,函数总会重新调用执行。 事件修饰符 Vue.js 为 v-on 提供了事件修饰符来处理 DOM 事件细节,如:event.preventDefault() 或 event.stopPropagation()。Vue.js通过由点(.)表示的指令后缀来调用修饰符。 <!-- 阻止单击事件冒泡 --> <a stop="doThis"></a> <!-- 提交事件不再重载页面 --> <form v-on:submit.prevent="onSubmit"></form> <!-- 修饰符可以串联 --> <a prevent="doThat"></a> <!-- 只有修饰符 --> <form v-on:submit.prevent></form> <!-- 添加事件侦听器时使用事件捕获模式 --> <div v-on:click.capture="doThis">...</div> <!-- 只当事件在该元素本身(而不是子元素)触发时触发回调 --> <div v-on:click.self="doThat">...</div> <!-- click 事件只能点击一次,2.1.4版本新增 --> <a 按键修饰符 为最常用的按键提供了别名: <input v-on:keyup.enter="submit"> <!-- 缩写语法 --> <input @keyup.enter="submit"> Vue.js为最常用的两个指令v-bind和v-on提供了缩写方式。v-bind指令可以缩写为一个冒号,v-on指令可以缩写为@符号。 全部的按键别名: .enter .tab .delete (捕获 "删除" 和 "退格" 键) .esc .space .up .down .left .right .ctrl .alt .shift .meta 实例 组件部分不太会,em...... 钩子函数 指令定义函数提供了几个钩子函数(可选): bind: 只调用一次,指令第一次绑定到元素时调用,用这个钩子函数可以定义一个在绑定时执行一次的初始化动作。 inserted: 被绑定元素插入父节点时调用(父节点存在即可调用,不必存在于 document )。 update: 被绑定元素所在的模板更新时调用,而不论绑定值是否变化。通过比较更新前后的绑定值,可以忽略不必要的模板更新(详细的钩子函数参数见下)。 componentUpdated: 被绑定元素所在模板完成一次更新周期时调用。 unbind: 只调用一次, 指令与元素解绑时调用。 钩子函数的参数有: el: 指令所绑定的元素,可以用来直接操作 DOM 。 binding: 一个对象,包含以下属性: name: 指令名,不包括 v- 前缀。 value: 指令的绑定值, 例如: v-my-directive="1 + 1", value 的值是 2。 oldValue: 指令绑定的前一个值,仅在 update 和 componentUpdated 钩子可用。无论值是否改变都可用。 expression: 绑定值的表达式或变量名。 例如 v-my-directive="1 + 1" , expression 的值是 "1 + 1"。 arg: 传给指令的参数。例如 v-my-directive:foo, arg 的值是 "foo"。 modifiers: 一个包含修饰符的对象。 例如: v-my-directive.foo.bar, 修饰符对象 modifiers 的值是 { foo: true, bar: true }。 vnode: Vue 编译生成的虚拟节点。 oldVnode: 上一个虚拟节点,仅在 update 和 componentUpdated 钩子可用。 样式叠加测试文件:test2/addStyle.html 属性覆盖测试文件:test2/cover.html 自定义组件测试文件:test2/customComponent.html get请求测试文件:test2/get.html post请求测试文件:test2/post.html vue初探:test2/helloVue.html,test2/helloVue2.html input 和 textarea 元素使用 v-model 实现双向数据绑定:test2/inputAndtextarea.html 两个按钮用于切换不同的列表布局:test2/layout.html 导航测试:test2/navigation.html 订单列表:test2/orderList.html 实时变更:test2/real-time-change.html 模糊搜索:test2/search.html 购物车:test2/shoppingCart.html 双向绑定:test2/two-way-binding.html 字符转换:test2/upperCase.html class属性绑定:test2/v-bind.html href 属性绑定:test2/v-test.html vue路由:test2/vueRouter.html watch监听时间:watchJianTing.html
优化这段代码<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>图书购物车</title> <style> </style> <script src="js/vue.js"></script> </head> <body> <div id="demo"> <table border="1"> <tr> <td></td> <td>书籍名称</td> <td>出版日期</td> <td>价格</td> <td>购买数量</td> <td>操作</td> </tr> <tr> <td></td> <td>{{books1.name}}</td> <td>{{books1.date}}</td> <td>¥{{books1.price}}</td> <td><button @click="down(books1)">-</button>{{books4.count}}<button @click="up(books1)">+</button></td> <td><button @click="del">移除</button></td> </tr> <tr> <td></td> <td>{{books2.name}}</td> <td>{{books2.date}}</td> <td>¥{{books2.price}}</td> <td> <button @click="down(books2)">-</button>{{books4.count}}<button @click="up(books2)">+</button> </td> <td><button @click="del">移除</button></td> </tr> <tr> <td></td> <td>{{books3.name}}</td> <td>{{books3.date}}</td> <td>¥{{books3.price}}</td> <td> <button @click="down(books3)">-</button>{{books4.count}}<button @click="up(books3)">+</button> </td> <td><button @click="del">移除</button></td> </tr> <tr> <td></td> <td>{{books4.name}}</td> <td>{{books4.date}}</td> <td>¥{{books4.price}}</td> <td> <button @click="down(books4)">-</button>{{books4.count}}<button @click="up(books4)">+</button> </td> <td><button @click="del">移除</button></td> </tr> </table> <div>总价: ¥{{sum}}</div> </div> <!-- js部分 --> <script> const vm = new Vue({ el: "#demo", data: { books1: { name: '《算法导论》', date: '2006-9', price: 85.00, count: 1 }, books2: { name: '《UNIX编程艺术》', date: '2006-2', price: 59.00, count: 1 }, books3: { name: '《编程珠玑》', date: '2008-10', price: 39.00, count: 1 }, books4: { name: '《代码大全》', date: '2006-3', price: 128.00, count: 1 } }, computed: { sum () { return this.books1.price * this.books1.count + this.books2.price * this.books2.count + this.books3.price * this.books3.count + this.books4.price * this.books4.count } }, methods: { down (books1) { this.books1.count = books1.count - 1; } } }) </script> </body> </html>
04-21
优化建议: 1. 使用数组来存储书籍信息,可以避免重复的代码和变量 2. 使用计算属性来计算总价,避免重复计算 3. 将操作封装成方法,并使用可复用的组件来渲染每一行 优化后的代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>图书购物车</title> <style> </style> <script src="js/vue.js"></script> </head> <body> <div id="demo"> <table border="1"> <tr> <td></td> <<td>书籍名称</td> <td>出版日期</td> <td>价格</td> <td>购买数量</td> <td>操作</td> </tr> <book-row v-for="(book, index) in books" :book="book" :index="index" :key="index" @change-count="changeCount" @remove-book="removeBook" /> </table> <div>总价: ¥{{totalPrice}}</div> </div> <!-- js部分 --> <script> Vue.component('book-row', { props: ['book', 'index'], methods: { down() { if (this.book.count > 1) { this.book.count--; this.$emit('change-count'); } }, up() { this.book.count++; this.$emit('change-count'); }, remove() { this.$emit('remove-book', this.index); } }, template: ` <tr> <td></td> <td>{{book.name}}</td> <td>{{book.date}}</td> <td>¥{{book.price}}</td> <td> <button @click="down">-</button> {{book.count}} <button @click="up">+</button> </td> <td> <button @click="remove">移除</button> </td> </tr> ` }); const vm = new Vue({ el: "#demo", data() { return { books: [ { name: '书籍1', date: '2022-01-01', price: 30, count: 1 }, { name: '书籍2', date: '2022-01-01', price: 40, count: 1 }, { name: '书籍3', date: '2022-01-01', price: 50, count: 1 }, { name: '书籍4', date: '2022-01-01', price: 60, count: 1 } ] }; }, methods: { changeCount() { // 重新计算总价 }, removeBook(index) { this.books.splice(index, 1); // 重新计算总价 }, }, computed: { totalPrice() { return this.books.reduce((acc, cur) => acc + cur.price * cur.count, 0); } } }); </script> </body> </html>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值