vue入门例子
vue入门例子
1、声明示渲染 {{message}}
2、绑定事件 v-bind
3、控制切换一个程序是否显示 v-if
4、渲染循环 v-for
5、点击事件 v-on
6、双向数据绑定 v-model
一、声明示渲染 {{message}} 例:
1 <template>
2 <div id="app">
3 <h1>
4 <p>{{message}}</p>
5 </h1>
6 </div>
7 </template>
8
9 <script>
10 export default {
11 name: 'app',
12 data(){
13 return{
14 message:'我最棒!'
15 }
16 },
17 }
18 </script>
19
20 <style scoped>
21 #app {
22 text-align: center;
23 color: red;
24 margin-top: 60px;
25 font-size: 60px;
26 }
27 </style>
效果如下:
二、绑定事件 v-bind 例:
<template>
<div id="app">
<h1>
<p v-bind:title="msg">
鼠标悬停几秒,查看动态绑定的提示信息!
</p>
</h1>
</div>
</template>
<script>
export default {
name: 'app',
data(){
return{
msg: '今天你吃早餐了吗' + new Date().toLocaleString()
}
},
}
</script>
<style scoped>
#app {
text-align: center;
color: red;
margin-top: 60px;
font-size: 60px;
}
</style>
显示效果如下:
请把鼠标悬停几秒,会有提示信息。
三、控制切换一个程序是否显示 v- for 例:
1 <template>
2 <div id="app">
3 <h1>
4 <p v-if="showMsg">大家好!</p>
5 </h1>
6 </div>
7 </template>
8
9 <script>
10 export default {
11 name: 'app',
12 data(){
13 return{
14 showMsg:true
15 }
16 },
17 }
18 </script>
19
20 <style scoped>
21 #app {
22 text-align: center;
23 color: red;
24 margin-top: 60px;
25 font-size: 60px;
26 }
27 </style>
效果如下:
四、渲染循环 v-for 例:
1 <template>
2 <div id="app">
3 <h3>
4 <ol>
5 <li v-for="list in lists">{{list.text}}</li>
6 </ol>
7 </h3>
8 </div>
9 </template>
10
11 <script>
12 export default {
13 name: "app",
14 data() {
15 return {
16 lists: [
17 { text: "天气晴朗,阳光明媚" },
18 { text: "适合约会!" },
19 { text: "不是吗?" }
20 ]
21 };
22 }
23 };
24 </script>
25
26 <style scoped>
27 #app {
28 text-align: center;
29 color: red;
30 margin-top: 60px;
31 font-size: 60px;
32 }
33 </style>
效果如下:
五、点击事件 v-on: 例:
1 <template>
2 <div id="app">
3 <h1>
4 <p>{{message}}</p>
5 </h1>
6 <button v-on:click='msg'>素素最美!</button>
7 </div>
8 </template>
9
10 <script>
11 export default {
12 name: 'app',
13 data(){
14 return{
15 message:'素素最棒!'
16 }
17 },
18 methods:{
19 msg:function(){
20 this.message = this.message.split('').reverse().join('')
21 }
22 }
23 }
24 </script>
25
26 <style scoped>
27 #app {
28 text-align: center;
29 color: red;
30 margin-top: 60px;
31 font-size: 60px;
32 }
33 </style>
点击按钮之后,字母可以反转,变为
六、双向数据绑定 v-model 例:
1 <template>
2 <div id="app">
3 <h1>{{msg}}</h1>
4 <input v-model="msg">
5 </div>
6 </template>
7
8 <script>
9 export default {
10 name: "app",
11 data() {
12 return {
13 msg:'海阔天空!'
14 };
15 }
16 };
17 </script>
18
19 <style scoped>
20 #app {
21 text-align: center;
22 color: red;
23 margin-top: 60px;
24 font-size: 60px;
25 }
26 </style>
效果如下:
尝试在input框里输入一些东西,查看效果,例:
有疑问,请加qq学习群:910316886
分类:
Vue
标签:
学习群:910316886
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现