Vue.js课后练习(登录注册和大小比较)

第一题

请编写登录页面和注册页面,通过动态组件实现动态切换页面中显示的组件,效果如图1和图2所示。

图1 登录页面

图2 注册页面

代码:

my.vue代码:

复制代码
<template>
	登录
</template>

<script setup>
</script>

<style>
</style>

you.vue代码:

复制代码
<template>
	注册
</template>

<script setup>
</script>

<style>
</style>

father.vue代码:

复制代码
<template>
	<button @click="suleng=my">登录</button>
	<button @click="suleng=you">注册</button>
	<div>
		<component :is="suleng"></component>
	</div>
</template>

<script setup>
	import my from './my.vue'
	import you from './you.vue'
	import {shallowRef} from 'vue'
	const suleng = shallowRef(my)
</script>

<style>
</style>

运行结果:

第二题

编程题1(请填写代码和运行结果截图)

请实现一个比较2个数大小的页面,当输入2个数字后,单击"比较"按钮后自动比较这2个数的大小,页面效果参考图1。比较数字大小结果显示的页面效果参考图2。

图1 比较2个数字大小的页面效果

图2 比较数字大小结果显示的页面效果

diyiti.vue代码:

复制代码
<template>
	<table>
		<tr>
			<td>
				<p>请输入第一个数字:</p>
				<!-- 使用input事件自定义获取指令one,获取input里面的内容为number1 -->
				<input type="number" @input="one" class="yi" />
			</td>
		</tr>
		<tr>
			<td>
				<p class="i">请输入第二个数字:</p>
				<input type="number" @input="two" class="er" />
			</td>
		</tr>
		<tr>
			<td>
				<!-- 给按钮定义点击事件,自定义comparison方法 -->
				<button @click="comparison">比较</button>
			</td>
		</tr>
		<tr>
			<!--双大号语法 转换响应式数据result -->
			<p>{{result}}</p>
		</tr>
	</table>
</template>

<script setup>
	import {ref} from 'vue'
	// 定义值number1 number2  使用let定义则后面使用获取值num

	let number1 = ref();
	let number2 = ref();
	// 使用const定义,使用number时后面需要加
	const result = ref('')
	// 自定义方法one
	const one = (event) => {
		// 获取input里面的内容为number1
		number1 = Number(event.target.value)
	}

	const two = (event) => {
		number2 = Number(event.target.value)
	}
	// 按钮点击执comparison(比较)方法
	const comparison = () => {
		// 使用if判断不同条件下给响应式数据结果的赋值
		if (number1 == number2) {
			// 响应式result数据结果的赋值
			result.value = '比较结果:两数相等';
		} else if (number1 > number2) {
			result.value = '比较结果:第一数大于第二个数';
		} else {
			result.value = '比较结果:第一数小于第二个数';
		}
	}
</script>

<style>
	
	.yi{
		position:absolute;
		left: 150px;
		top: 30px;
	}
	.er{
		position: absolute;
		left: 150px;
		top: 55px;
		
	}
	.i{
		position: absolute;
		top: 45px;
	}
	button{
		position: absolute;
		top: 120px;
	}
</style>

运行结果:

今天就分享到此,感谢预览~

相关推荐
Mh3 小时前
别再只会 `find` 了:Map 在前端业务里的真实用法
前端·javascript
陈随易4 小时前
FFmpeg 9.0 发布,代号 Lei,音视频处理再升级
前端·后端·程序员
爱丶狸4 小时前
Grafana_Zabbix_ImageRenderer_部署与前端操作手册
linux·前端·zabbix·grafana·kylin
用户059540174467 小时前
把AI对话记忆存储测试从手工改成Playwright+pytest,覆盖率从20%提到96%,回归时间缩短90%
前端·css
kyriewen7 小时前
别再这样写TypeScript了——Code Review中最常见的8个反模式
前端·javascript·typescript
名字还没想好☜7 小时前
Next.js ‘use client‘ 到底加在哪:Server/Client Components 边界与常见报错
开发语言·前端·javascript·react·next.js
午安~婉8 小时前
GitHub Token/ GitHub Stats统计显示图异常
前端·github·vercel·github token
码云之上8 小时前
AI Agent 工程化总览篇:从 Prompt 到 Harness
前端·人工智能
2501_926978338 小时前
以说明书 DNA 为模板——完整 AGI 的结构图景
前端·人工智能·经验分享·笔记·ai写作
IT_陈寒8 小时前
Vite静态资源引用这个坑我踩得有点疼
前端·人工智能·后端