vueå¦ä¹ è®°å½ä¸ï¼æ令ï¼
程序员文章站
2022-05-16 18:49:34
...
ä¸ï¼ä»ç»
Vue.jsæ¯ä»ä¹ï¼
Vue.jsæ¯ä¸å¥ç¨äºæ建ç¨æ·çé¢çæ¸è¿å¼æ¡æ¶ãVue çæ ¸å¿åºåªå ³æ³¨è§å¾å±ï¼ä¸ä» æäºä¸æï¼è¿ä¾¿äºä¸ç¬¬ä¸æ¹åºææ¢æ项ç®æ´åãå¦ä¸æ¹é¢ï¼å½ä¸ç°ä»£åçå·¥å ·é¾ä»¥ååç§æ¯æç±»åºç»å使ç¨æ¶ï¼Vue ä¹å®å ¨è½å¤ä¸ºå¤æçå页åºç¨æä¾é©±å¨ã
äºï¼å®è£
1.CDNå®è£ ï¼
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
2.NPMå®è£
D:\è¹ævue\è¹æ1011>npm init
D:\è¹ævue\è¹æ1011>npm install vue --save
ä¸ï¼åºæ¬æ令
1.v-modelï¼æ¯ä¸ä¸ªæ°æ®ååç»å®çæ令ï¼ã
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="../è¹æ1011/node_modules/vue/dist/vue.min.js"></script>
</head>
<body>
<div id="ss">
<input type="text" v-model='msg'>
<p>{{msg}}</p>
</div>
<script>
let vm = new Vue({
el: "#ss",
data: {
msg: 'hi'
}
})
</script>
</body>
</html>
2.v-if
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="../node_modules/vue/dist/vue.min.js"></script>
</head>
<body>
<div id="ss">
<p v-if='seen'>å¯ä»¥çå°æ</p>
</div>
<script>
let vm = new Vue({
el: '#ss',
data: {
seen:true
}
})
</script>
</body>
</html>
3.v-for
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="../node_modules/vue/dist/vue.min.js"></script>
</head>
<body>
<div id="ss">
<ol>
<li v-for="item in msg">
{{item.text}}
</li>
</ol>
</div>
<script>
let vm = new Vue({
el: '#ss',
data: {
msg: [
{text:'ä½ å¥½'},
{text:'ä¸ç'},
{text:'ä½ å¥½'},
{text:'ä¸å½'}
]
}
})
</script>
</body>
</html>
4.v-html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="../node_modules/vue/dist/vue.min.js"></script>
</head>
<body>
<div id="ss" v-html='msg'>
</div>
<script>
let vm = new Vue({
el: "#ss",
data: {
msg: '<h1>ä½ å¥½</h1>'
}
})
</script>
</body>
</html>
5.v-text
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="../node_modules/vue/dist/vue.min.js"></script>
</head>
<body>
<div id="ss" v-text='msg'>
</div>
<script>
let vm = new Vue({
el: "#ss",
data: {
msg: '<h1>ä½ å¥½</h1>'
}
})
</script>
</body>
</html>
6.v-cloak
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
[v-cloak] {
display: none;
}
</style>
</head>
<body>
<div id="ss" v-cloak>
<span>{{msg}}</span>
<span>{{msg}}</span>
<span>{{msg}}</span>
<span>{{msg}}</span>
<span>{{msg}}</span>
</div>
<script src="../node_modules/vue/dist/vue.min.js"></script>
<script>
let vm = new Vue({
el: "#ss",
data: {
msg: 'yes'
}
})
</script>
</body>
</html>
7.v-on
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="../node_modules/vue/dist/vue.min.js"></script>
</head>
<body>
<div id="ss">
<p>{{msg}}</p>
<button @click='reverse'>å转信æ¯</button>
</div>
<script>
let vm = new Vue({
el: '#ss',
data: {
msg: 'hello world',
},
methods: {
reverse() {
this.msg = this.msg.split('').reverse().join();
}
}
})
</script>
</body>
</html>
8.v-bind
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="../node_modules/vue/dist/vue.min.js"></script>
<style>
img{
width: 500px;
height: 500px;
}</style>
</head>
<body>
<div id="ss">
<img :src="msgSrc" alt="">
</div>
<script>
let vm = new Vue({
el:'#ss',
data:{
msgSrc:"m2.jpg",
}
})
</script>
</body>
</html>