【Vue学习笔记】—— vue的基础语法 { }
学习笔记
作者:oMing
vue
v-on: 简称 @
<div id='app'>
<button v-on:click='Show1'> </button>
<button @click='Show2'> </button>
</div>
new Vue({
methods: {
show1(){
console.log("show1")
},
show2(){
console.log("show2")
}
}
}).$mount('#app')
v-model(数据双向绑定)
<div>
<input type="text" v-model='name'>
</div>
var vm = new Vue({
el: '#app',
data: {
name: ''
}
}
- 只要 input 内容发生变化,vue app 实例的 data 中的数据也会发生变化,反之亦然


v-bind: (给元素的 ++属性++ 绑定数据)
<div id='app'>
<h1 v-bind:class="{ white: canWhite }"</h1>
</div>
.white {
color: #fff;
}
new Vue({
data: {
canWhite: false,
}
}).$mount('#app')
{{ }}、v-text=" " 、v-html=" " (给 ++元素++ 绑定数据)
[v-cloak] {
display: none;
}
<!-- v-cloak 解决插值表达式闪烁的问题 -->
<p v-cloak>{{ data }}</p>
<p v-text='data'></p>
<p v-html='data'></p>
v-for
<div id="app">
<input type="text" v-model='name'>
<input type="submit" @click.prevent='add'>
<ul>
<li v-for='(person,index) in persons' :key='person.id'><input type="checkbox">{{ person.name }}</li>
</ul>
</div>
new Vue({
el: '#app',
data: {
name: '',
id: 0,
persons: [
{name: 'javascript',id: 1},
{name: 'Vue',id: 2},
{name: 'jQuery',id: 3}
]
},
methods: {
add(){
let lastId = ++this.id
let newObj = {name: this.name,id: lastId}
this.persons.unshift(newObj)
this.name = ''
}
},
created(){
this.id = this.persons[this.persons.length-1].id
}
});
组件(component)
- 创建组件
- 方式1
<div id='app'>
<!-- 使用标签的形式使用已创建好的组件 -->
<my-com1></my-com1>
<my_com2></my_com2>
<my_com3></my_com3>
</div>
<!-- 在被控制的 app 外面,使用 template 元素创建组件的 HTML 模板结构 -->
<template id="tmp_1">
<div>
<h1>这是通过 template 创建的组件(myCom3),有代码提示 --- {{ msg }}</h1>
<span>非常好用!</span>
</div>
</template>
var com1 = Vue.extend({
template: '<h1>这是使用 Vue.extend 创建的组件(myCom1)</h1>'
})
// 在注册 组件 的时候使用 “驼峰命名法”
// 在使用组件的时候就要使用 “短横线分割命名法”(my-com1)
Vue.component("myCom1",com1)
- 方式2
Vue.component("my_com2",{
template: "<div><h1>这是直接使用 Vue.component 创建的组件(myCom2)</h1><span>有且只有一个'根元素'div</span></div>"
})
- 方式3
Vue.component("my_com3",{
template: "#tmp_1",
// 组件中的 data
data: function(){
return {
msg: "这是组件中 data 定义的数据"
}
},
})
- 渲染组件的另一种方式
new Vue({
// 注意,这种方式和使用标签渲染的方式只能选择其中一种
render: function(createElement){
let myCom1 = createElement("myCom1")
return myCom1
},
})
插槽 slot
<!-- 匿名插槽 -->
<div class="app">
<father></father>
</div>
<template id="father">
<div>
<!-- 在使用子组件的时候 动态 给子组件添加内容 -->
<son>
<div>我是son中的追加数据1</div>
<div>我是son中的追加数据2</div>
<div>我是son中的追加数据3</div>
</son>
</div>
</template>
<template id="son">
<div>
<div>我是头部</div>
<slot>我是默认数据</slot>
<div>我是尾部</div>
</div>
</template>
<!-- 具名插槽 -->
<div class="app">
<father></father>
</div>
<template id="father">
<div>
<!-- 在使用子组件的时候 动态 给子组件添加内容 -->
<son>
<div slot="one">我是son中one的追加数据</div>
<div slot="two">我是son中two的追加数据</div>
</son>
</div>
</template>
<template id="son">
<div>
<div>我是头部</div>
<slot name="one">我是one默认数据</slot>
<slot name="two">我是two默认数据</slot>
<div>我是尾部</div>
</div>
</template>
<!-- v-slot插槽 -->
<div class="app">
<father></father>
</div>
<template id="father">
<div>
<!-- 在使用子组件的时候 动态 给子组件添加内容 -->
<son>
<template v-slot:one>
<div>我是son中one的追加数据</div>
</template>
<template v-slot:two>
<div>我是son中two的追加数据</div>
</template>
<!-- #default == v-slot:default="scope" default用于匿名插槽-->
<!-- <template #default="scope">
<div>names --- {{ scope.names }}</div>
<li v-for="(name,i) in scope.names" :key="i">{{ name }}</li>
</template> -->
<!-- 具名的v-slot 插槽名字为names -->
<template #names="scope">
<div>names --- {{ scope.names }}</div>
<li v-for="(name,i) in scope.names" :key="i">{{ name }}</li>
</template>
</son>
</div>
</template>
<template id="son">
<div>
<div>我是头部</div>
<slot name="one">我是one默认数据</slot>
<slot name="two">我是two默认数据</slot>
<slot name="names" v-bind:names="names">我是names默认数据</slot>
<div>我是尾部</div>
</div>
</template>
<!-- 作用域插槽 -->
<div class="app">
<father></father>
</div>
<template id="father">
<div>
<!-- 在使用子组件的时候 动态 给子组件添加内容 -->
<son>
<!-- slot-scope="scope"的作用:接收子组件暴露的数据 -->
<template slot-scope="scope">
<div>names: {{ scope.names }}</div>
<li v-for="(name,i) in scope.names" :key="i">{{ name }}</li>
</template>
</son>
</div>
</template>
<template id="son">
<div>
<div>我是头部</div>
<!-- v-bind:names="names"的作用:将当前子组件的names数据暴露给父组件 -->
<slot v-bind:names="names">我是one默认数据</slot>
<!-- <slot :names="names">我是one默认数据</slot> -->
<div>我是尾部</div>
</div>
</template>
Vue.component("father",{
template: "#father",
components: {
"son": {
template: "#son",
data: function(){
return {
names: ['oMing','Unity','Vue','JS'],
}
},
}
},
})
new Vue({
el: ".app",
})
vue-router 路由
<div id="app">
<!-- 第一种赋值方式 -->
<router-link to="/login?name=oMing&age=21" tag="button">登录</router-link>
<!-- 第二种赋值方式 -->
<router-link to="/register/vue/5" tag="button">注册</router-link>
<transition mode="out-in">
<router-view></router-view>
</transition>
</div>
// 1、定义组件
const login = {
template: "<h1>登录界面</h1>",
// 生命周期方法
created(){
console.log(this.$route)
// 第一种获取方式
console.log(this.$route.query.name + " " + this.$route.query.age)
}
}
const register = {
template: "<h1>注册界面</h1>",
// 生命周期方法
created(){
console.log(this.$route)
// 第二种获取方式
console.log(this.$route.params.name + " " + this.$route.params.age)
}
}
// 2、根据自定义的路由规则创建 路由对象
const vue_router = new VueRouter({
// 3、定义路由(切换规则)
routes: [
// 数组中的每一个对象就是一条规则
{ path: '/login', component: login },
// 第二种赋值方式
{ path: '/register/:name/:age', component: register },
// 路由重定向
{ path: '/', redirect: '/login' }
],
// 修改 router-link 的类名
linkActiveClass: "lr-active",
})
new Vue({
// 4、将创建好的 路由对象 绑定 到 app实例
router: vue_router,
}).$mount("#app")
过滤器(格式化文本内容)
- 把 # 替换为 vue
<div id='app'>
// 不需要传message
<p>{{ message | messageFormat('vue') }}</p>
</div>
Vue.filter('messageFormat',function(message,arg){
return message.replace(/#/g,arg)
})
new Vue({
el: '#app',
data: {
message: 'Fireming love #,# is great'
}
})
自定义指令(使用 Vue.directive() 定义全局的 指令)
- 全局
<div id='app'>
<input type="text" v-focus v-color="'blue'" v-fontweight="1000">
</div>
Vue.directive("focus", {
// el 是一个原生的 js 对象,DOM对象
// 当指令绑定到元素上的时候触发
bind: function (el) {
},
// 当绑定元素 input 插入到 DOM 中的时候触发
inserted: function (el) {
// 聚焦元素
el.focus()
},
// 当 VNode 更新的时候触发
updated: function () {
}
})
Vue.directive("color", {
bind: function (el, binding) {
el.style.color = binding.value
}
})
- 局部
new Vue({
el: '#app',
// 自定义私有 指令
directives: {
"fontweight": function (el, binding) {
el.style.fontWeight = binding.value
}
}
})
自定义按键修饰符
<input v-on:keyup.113="submit">
// 自定义全局按键修饰符
Vue.config.keyCodes.f2 = 113
watch (用于监听数据变化)
<div id='app'>
<input type="text" v-model:value='num'>
</div>
new Vue({
el: '#app',
data: {
num: 1,
}
watch: {
num: function(newValue,oldValue){
console.log(newValue,oldValue)
}
}
})
【Vue学习笔记】—— vue的基础语法 { }的更多相关文章
- Vue学习笔记-Vue.js-2.X 学习(二)===>组件化开发
===重点重点开始 ========================== (三) 组件化开发 1.创建组件构造器: Vue.extends() 2.注册组件: Vue.component() 3.使用 ...
- Vue学习笔记-Vue.js-2.X 学习(一)===>基本知识学习
一 使用环境: windows 7 64位操作系统 二 IDE:VSCode/PyCharm 三 Vue.js官网: https://cn.vuejs.org/ 四 下载安装引用 方式1:直接 ...
- Vue学习笔记-Vue.js-2.X 学习(六)===>脚手架Vue-CLI(项目说明-Babel)
五 Vue学习-vue-cli脚手架学习(创建只选一个选项:Babel) 1. 项目目录说明 node_modules : 包管理文件夹 public : 静态资源 src : 源代码 gitign ...
- Vue学习笔记-Vue.js-2.X 学习(四)===>脚手架Vue-CLI(基本工作和创建)
(五) 脚手架Vue-CLI 一 Vue-CLI前提(nodejs和webpack) 二 Vue学习-nodejs按装配置,Node.js 就是运行在服务端的 JavaScript. 1. 去nod ...
- Vue学习笔记-Vue.js-2.X 学习(五)===>脚手架Vue-CLI(PyCharm)
Vue项目在pycharm中配置 退出运行: ctrl+c Vue学习笔记-Vue.js-2.X 学习(六)===>脚手架Vue-CLI(项目说明)
- Vue学习笔记-Vue.js-2.X 学习(三)===>组件化高级
(四) 组件化高级 1.插槽(slot)的基本使用 A:基本使用: <slot></slot> B:默认置:<slot><h1>中间可以放默认值< ...
- Vue学习笔记-Vue基础入门
此篇文章是本人在学习Vue是做的部分笔记的一个整理,内容不是很全面,希望能对阅读文章的同学有点帮助. 什么是Vue? Vue.js (读音 /vjuː/,类似于 view) 是一套构建用户界面的渐进式 ...
- 【Vue学习笔记】—— vuex的语法 { }
学习笔记 作者:o_Ming vuex Vuex ++ state ++ (用于存储全局数据) 组件访问 state 中的全局数据的方式1: this.$store.state.全局数据 组件访问 s ...
- <学习笔记 之 JQuery 基础语法>
jQuery 库 - 特性 jQuery 是一个 JavaScript 函数库. jQuery 库包含以下特性: HTML 元素选取 HTML 元素操作 CSS 操作 HTML 事件函数 JavaSc ...
- Vue学习笔记-Vue.js-2.X 学习(七)===>脚手架Vue-CLI(路由Router)
脚手架Vue-CLI(路由Router) 一 按装(通过新创建脚手架按装),如果在原来的脚手架上按装直接进图型化界面vue ui的插件按装. 二 使用(上面按装下面步骤自动会生成) 第一步:导入路由对 ...
随机推荐
- ansible在linux和windows批量部署zabbix-agent2
--- - hosts: linux tasks: - name: copy centos 7 zabbix-agent2 copy: src=zabbix-agent2-5.0.11-1.el7.x ...
- Python收集这些视频只是单纯的想做做壁纸,大家不要误会
首先澄清一下,我用Python收集这些视频,绝不是想做别的什么,真的只是用来做动态壁纸,大家不要误会!我不是那样的人~ 这样的不过份吧 (这个动图看不看的到就看有没有缘分了 ) 阅读本文你需要准备 1 ...
- 一个紧张刺激的聊天器,要不要进来看看(Python UDP网络模型)
先来哔哔两句:(https://jq.qq.com/?_wv=1027&k=QgGWqAVF) 互联网的本质是什么?其实就是信息的交换.那么如何将自己的信息发送到其他人的电脑上呢?那就需要借助 ...
- LeetCode. 812. 最大三角形面积
812. 最大三角形面积 鞋带公式 鞋带公式,用于计算任意多边形的面积,可用于计算三角形的面积 已知 ΔABC 三个顶点的坐标 A:(x1,y1). B:(x2,y2). C:(x3,y3) 对应的矩 ...
- NC15052 求最值
NC15052 求最值 题目 题目描述 给你一个长为 \(n\) 的序列 \(a\) 定义 \(f(i,j)=(i-j)^2+g(i,j)^2\) \(g\) 是这样的一个函数 求最小的 \(f(i, ...
- NuGetTools:批量上传、删除和显示NuGet包
快照 前言 NuGet是.NET开发必不可少的包管理工具,在日常更新版本过程中,可能需要批量发布 NuGet 包,也不可避免需要发布一些测试的包,后期想将这些测试或者过期的包删除掉.nuget.org ...
- 启动docker报错Failed to listen on Docker Socket for the API.
1.启动时报错查看日志发现 # journalctl -xe Failed to listen on Docker Socket for the API. 查找socket这个配置文件,修改如下 # ...
- 解决报错Error response from daemon: Get https://10.0.0.110/v2/: dial tcp 10.0.0.110:443: connect: connection refused
修改 #https不需要验证,否则要加上以下配置# 意思就是非安全仓库,加上重启就OK了! vim /lib/systemd/system/docker.service --insecure-regi ...
- grafana监控配置
一.配置开启smtp服务 1.编辑grafana配置文件grafana.ini [smtp] enabled = true host = smtp.163.com:25 user = 157xxxx3 ...
- 使用.NET简单实现一个Redis的高性能克隆版(一)
译者注 该原文是Ayende Rahien大佬业余自己在使用C# 和 .NET构建一个简单.高性能兼容Redis协议的数据库的经历. 首先这个"Redis"是非常简单的实现,但是他 ...