学习笔记

  • 作者: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的基础语法 { }的更多相关文章

  1. Vue学习笔记-Vue.js-2.X 学习(二)===>组件化开发

    ===重点重点开始 ========================== (三) 组件化开发 1.创建组件构造器: Vue.extends() 2.注册组件: Vue.component() 3.使用 ...

  2. Vue学习笔记-Vue.js-2.X 学习(一)===>基本知识学习

    一  使用环境: windows 7 64位操作系统 二  IDE:VSCode/PyCharm 三  Vue.js官网: https://cn.vuejs.org/ 四  下载安装引用 方式1:直接 ...

  3. Vue学习笔记-Vue.js-2.X 学习(六)===>脚手架Vue-CLI(项目说明-Babel)

    五  Vue学习-vue-cli脚手架学习(创建只选一个选项:Babel) 1. 项目目录说明 node_modules : 包管理文件夹 public : 静态资源 src : 源代码 gitign ...

  4. Vue学习笔记-Vue.js-2.X 学习(四)===>脚手架Vue-CLI(基本工作和创建)

    (五) 脚手架Vue-CLI 一 Vue-CLI前提(nodejs和webpack) 二  Vue学习-nodejs按装配置,Node.js 就是运行在服务端的 JavaScript. 1. 去nod ...

  5. Vue学习笔记-Vue.js-2.X 学习(五)===>脚手架Vue-CLI(PyCharm)

    Vue项目在pycharm中配置 退出运行: ctrl+c Vue学习笔记-Vue.js-2.X 学习(六)===>脚手架Vue-CLI(项目说明)

  6. Vue学习笔记-Vue.js-2.X 学习(三)===>组件化高级

    (四) 组件化高级 1.插槽(slot)的基本使用 A:基本使用: <slot></slot> B:默认置:<slot><h1>中间可以放默认值< ...

  7. Vue学习笔记-Vue基础入门

    此篇文章是本人在学习Vue是做的部分笔记的一个整理,内容不是很全面,希望能对阅读文章的同学有点帮助. 什么是Vue? Vue.js (读音 /vjuː/,类似于 view) 是一套构建用户界面的渐进式 ...

  8. 【Vue学习笔记】—— vuex的语法 { }

    学习笔记 作者:o_Ming vuex Vuex ++ state ++ (用于存储全局数据) 组件访问 state 中的全局数据的方式1: this.$store.state.全局数据 组件访问 s ...

  9. <学习笔记 之 JQuery 基础语法>

    jQuery 库 - 特性 jQuery 是一个 JavaScript 函数库. jQuery 库包含以下特性: HTML 元素选取 HTML 元素操作 CSS 操作 HTML 事件函数 JavaSc ...

  10. Vue学习笔记-Vue.js-2.X 学习(七)===>脚手架Vue-CLI(路由Router)

    脚手架Vue-CLI(路由Router) 一 按装(通过新创建脚手架按装),如果在原来的脚手架上按装直接进图型化界面vue ui的插件按装. 二 使用(上面按装下面步骤自动会生成) 第一步:导入路由对 ...

随机推荐

  1. UiPathExcel读取操作

    一.Uipath操作Excel的相关基本概念 1.UiPath操作Excel的两组方法 App Integration > Excel   VS  System > File > W ...

  2. C4C界面介绍

    by zyi

  3. UI自动化测试执行问题总结

    ------------恢复内容开始------------ ![image](https://img2022.cnblogs.com/blog/1510476/202206/1510476-2022 ...

  4. 字符输入流_Reader类&FileReader类介绍和字符输入读取字符数据

    java.io.Reader:字符输入流的最顶层父类,其中定义了一些共性的成员方法,是一个抽象类共性的成员方法 int read() 读取单个字符并返回 int read(char[] cbuf) 将 ...

  5. Solution -「CF113D」Museum

    Upd 2021.10.21 更改了状态定义. 记 \(S(u)\) 表示 \(u\) 结点的相邻结点的集合. 又记 \(p(u)\) 表示走到了 \(u\) 且下一步继续留在 \(u\) 结点的概率 ...

  6. Kubernetes组件介绍

    一.api-server   基本概念 该端口默认值为6443,可通过启动参数"--secure-port"的值来修改默认值. 默认IP地址为非本地(Non-Localhost)网 ...

  7. CF242E XOR on Segment

    CF242E XOR on Segment codeforces 洛谷 关于异或,无法运用懒标记实现区间异或: 可以像trie树一样拆位,将每个值拆成二进制数,对此建相应个数的线段树. 0 1与 0异 ...

  8. 利用MySQL Router构建读写分离MGR集群

    GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源. 目录 1. 部署MySQL Router 2. 启动mysqlrouter服务 3. 确认读写分离效果 4. 确认只读负载 ...

  9. P4715 淘汰赛 - 记录

    P4715 淘汰赛 题目描述 有 2^n (n≤7) 个国家参加世界杯决赛圈且进入淘汰赛环节.我经知道各个国家的能力值,且都不相等.能力值高的国家和能力值低的国家踢比赛时高者获胜.1 号国家和 2 号 ...

  10. LuoguP5022 旅行 (割点,基环树)

    // luogu-judger-enable-o2 #include <cstdio> //#include <iostream> #include <cstring&g ...