目录
vue中的自定义指令通过Vue.directive来实现,主要完成内置指令不能完成的一些事情
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Vue入门之自定义指令title>
- <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
- head>
- <body>
- <div id="app">
- <div v-test="color">
- {{num}}
- div>
- div>
- <button onclick="unbindApp()">解绑button>
-
- <script type="text/javascript">
- // 解绑
- function unbindApp() {
- app.$destroy();
- }
-
- // 自定义指令
- Vue.directive("test",{
- //1-被绑定
- bind:function (el, binding, vnode) {
- console.log("1-bind 被绑定");
- console.log("el:",el);
- console.log("binding:",binding);
- console.log("vnode:",vnode);
- el.style.color = binding.value;
- },
- //2-被插入
- inserted:function (el, binding, vnode) {
- console.log("2-inserted 被插入");
- },
- //3-更新
- update:function (el, binding, vnode) {
- console.log("3-update 更新");
- },
- //4-更新完成
- componentUpdated:function (el, binding, vnode) {
- console.log("4-componentUpdated 更新完成");
- },
- //5-解绑
- unbind:function (el, binding, vnode) {
- console.log("5-unbind 解绑");
- }
- });
-
- var app = new Vue({
- el:'#app',
- data:{
- num: 123,
- color:'red'
- }
- })
- script>
- body>
- html>
(1)chrome打开控制器查看
(2)控制台输入“app.num=’通过控制台设置的新name’”
(3)点击解绑按钮
自定义指令有五个生命周期(也叫钩子函数),分别是bind、inserted、update、componentUpdated、unbind,说明如下:
(1)全局注册
- // script
- Vue.component('button-counter', {
- data: function () {
- return {
- count: 0
- }
- },
- template: ''
- });
-
- new Vue({
- el: '#app'
- });
-
- // html使用
- <button-counter>button-counter>
(2)局部注册
- // script
- new Vue({
- el: '#app',
- components:{
- "button-inner":{
- data: function() {
- return {
- inner: 0
- }
- },
- template: ''
- }
- }
- });
-
- // html使用
- <button-inner>button-inner>
(1)属性取值
- // script
- new Vue({
- el: '#app',
- components:{
- "button-props":{
- template:`参数1: {{ here }}:---参数2: {{fromHere}}`,
- props:['here', 'fromHere']
- }
- }
- });
-
- // html使用
- <button-props here="hello" from-here="world">button-props>
PS:如果属性带“-”,props中需要驼峰取值
(2)在构造器向组件传值(v-bind)
- // script
- new Vue({
- el: '#app',
- data: {
- message: 'hello'
- },
- components:{
- "button-props":{
- template:`参数1: {{ here }}:---参数2: {{fromHere}}`,
- props:['here', 'fromHere']
- }
- }
- });
-
- // html使用
- <button-props v-bind:here="message" :from-here="message">button-props>
- // script
- // 子组件
- var city = {
- template:`Sichuan of China`
- }
- // 父组件
- var parent = {
- template:
- `
-
Panda from China!
-
- `,
- components:{
- "city": city
- }
- }
-
- // 实例化
- new Vue({
- el: '#app',
- // 定义局部组件
- components:{
- // 组件注册
- "parent": parent
- }
- });
-
- // html使用
- <parent>parent>
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Vue入门之组件title>
- <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
- head>
- <body>
- <div id="app">
-
- <div>
- <button-counter>button-counter>
- div>
-
- <div>
- <button-inner>button-inner>
- div>
-
- <div>
- <button-props here="hello" from-here="world">button-props>
- div>
-
- <div>
- <button-props v-bind:here="message" :from-here="message">button-props>
- div>
-
- <div>
- <parent>parent>
- div>
-
- div>
-
- <script type="text/javascript">
- // 定义全局组件
- Vue.component('button-counter', {
- data: function() {
- return {
- count: 0
- }
- },
- template: ''
- });
-
- // 子组件
- var city = {
- template: `Sichuan of China`
- }
- // 父组件
- var parent = {
- template: `
-
Panda from China!
-
- `,
- components: {
- "city": city
- }
- }
-
- // 实例化
- new Vue({
- el: '#app',
- data: {
- message: 'hello'
- },
- // 定义局部组件
- components: {
- "button-inner": {
- data: function() {
- return {
- inner: 0
- }
- },
- template: ''
- },
- // 取值
- "button-props": {
- template: `参数1: {{ here }}:---参数2: {{fromHere}}`,
- props: ['here', 'fromHere']
- },
- // 组件注册
- "parent": parent
- }
- });
- script>
- body>
- html>
vue中的模板使用template来实现
- "app">
-
- <script type="text/javascript">
- // 实例化
- new Vue({
- el: '#app',
- data: {
- message: 'hello'
- },
- template:`
我是选项模板
` - });
- script>
- "app">
- <template id="demo2">
- <h2 style="color:red">我是template标签模板h2>
- template>
-
- <script type="text/javascript">
- // 实例化
- new Vue({
- el: '#app',
- data: {
- message: 'hello'
- },
- template:'#demo2'
- });
- script>