三种方法实现姓名案例对比vue(插值语法/methods/计算属性)

插值语法

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script type="text/javascript" src="js/vue.js">
			
		</script>
	</head>
	<body>
		<div id="root">
			姓:<input type="text" v-model="firstName"/>
			<br>
			<br>
			名:<input type="text" v-model="lastName"/>
			<br>
			
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 中,你可以使用插值语法(即双大括号 `{{ }}`)来动态绑定标签名字。具体实现步骤如下: 1. 在 Vue 组件中,定义一个名为 `tag` 的属性,用来存储标签名字。 2. 在模板中,使用双大括号 `{{ }}` 将 `tag` 属性绑定到标签名字上。 下面是一个示例代码,用来在 Vue实现动态标签名字的效果: HTML代码: ``` <template> <div> <button @click="changeTag('div')">div</button> <button @click="changeTag('span')">span</button> <button @click="changeTag('p')">p</button> <button @click="changeTag('h1')">h1</button> <button @click="changeTag('h2')">h2</button> <button @click="changeTag('h3')">h3</button> <button @click="changeTag('h4')">h4</button> <button @click="changeTag('h5')">h5</button> <button @click="changeTag('h6')">h6</button> <button @click="changeTag('button')">button</button> <button @click="changeTag('a')">a</button> <button @click="changeTag('input')">input</button> <button @click="changeTag('img')">img</button> <button @click="changeTag('label')">label</button> <button @click="changeTag('textarea')">textarea</button> <button @click="changeTag('select')">select</button> <button @click="changeTag('option')">option</button> <button @click="changeTag('ul')">ul</button> <button @click="changeTag('li')">li</button> <button @click="changeTag('table')">table</button> <button @click="changeTag('tr')">tr</button> <button @click="changeTag('td')">td</button> <button @click="changeTag('th')">th</button> <button @click="changeTag('form')">form</button> <button @click="changeTag('br')">br</button> <button @click="changeTag('hr')">hr</button> <button @click="changeTag('i')">i</button> <button @click="changeTag('u')">u</button> <button @click="changeTag('b')">b</button> <button @click="changeTag('em')">em</button> <button @click="changeTag('strong')">strong</button> <button @click="changeTag('code')">code</button> <button @click="changeTag('pre')">pre</button> <button @click="changeTag('blockquote')">blockquote</button> <button @click="changeTag('cite')">cite</button> <button @click="changeTag('q')">q</button> <button @click="changeTag('abbr')">abbr</button> <button @click="changeTag('del')">del</button> <button @click="changeTag('ins')">ins</button> <button @click="changeTag('kbd')">kbd</button> <button @click="changeTag('mark')">mark</button> <button @click="changeTag('s')">s</button> <button @click="changeTag('sub')">sub</button> <button @click="changeTag('sup')">sup</button> <component v-bind:is="tag">这是一个{{ tag }}标签</component> </div> </template> <script> export default { data() { return { tag: 'div' } }, methods: { changeTag(newTag) { this.tag = newTag } } } </script> ``` 在上面的代码中,通过 `v-bind` 指令动态绑定 `is` 属性为 `tag` 的值,从而动态切换组件的标签名字。通过 `changeTag` 方法来改变 `tag` 属性的值,从而实现动态切换标签名字的效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值