1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
| <!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> vue 进阶系列之全局 API 的使用</title> <style> .todo-list { margin: 100px auto; width: 500px; text-align: center; }
.display-item { text-align: left; }
.input-todo-item>input { width: 300px; height: 35px; border: 1px solid #bbb; border-radius: 4px; padding-left: 1rem; }
.input-todo-item>input:focus { border-radius: 4px; }
.input-todo-item>button { line-height: 35px; color: #ffffff; background-color: red; outline: none; opacity: .6; border-radius: 4px; border: none; padding: 0 1rem; }
.input-todo-item>button:hover { opacity: 1; }
.disableBtn { background-color: #bbb !important; opacity: 1 !important; } </style> </head>
<body> <div id="mount-point"> <div> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script> const inputTpl = `<div class="input-todo-item"> <input type="text" v-model="inputValue"/> <button ref="addbtn" @click="addClick" :disabled="disableAddBtn" :class="{disableBtn: disableAddBtn}"> add </button> </div> ` const disTpl = `<div class="display-item"> <ul> <li v-for="(item, index) in disList" :key="index" :style="{textDecorationLine: item.isCompleted ? 'line-through' : 'none'}" @click="deleteItem"> {{item.text}} </li> </ul> </div>` const tpl = `<div class="todo-list"> <input-item @onAddItem="addItemToList"/> <display-item :disList="todoLists"/> </div>` const DisplayItem = Vue.extend({ template: disTpl, props: { disList: { type: Array } }, methods: { deleteItem() { } } }) Vue.component('display-item', DisplayItem) Vue.component('input-item', { template: inputTpl, data() { return { inputValue: '' } }, computed: { disableAddBtn() { return !this.inputValue } }, methods: { addClick() { console.log(this.disableAddBtn) if (this.disableAddBtn) return this.$emit('onAddItem', this.inputValue) } } }) const TodoList = new Vue({ template: tpl, props: { data: { type: Object } }, data() { return { todoLists: [ { text: '这个周末去song' } ] } } }) let list = TodoList.$data.todoLists list = list.map(item => { TodoList.$set(item, 'isCompleted', true) return item }) TodoList.addItemToList = value => { list.push({ isCompleted: false, text: value }) TodoList.$data.todoLists = list } TodoList.$mount('#mount-point')
console.log(TodoList.$data) console.log(TodoList.$options) // => {components: {…}, directives: {…}, filters: {…}, _base: ƒ, template: "<div class="todo-list"><input-item @o…play-item :disList="todoLists"/>↵ </div>", …} console.log(TodoList.$el) console.log(TodoList.$root) console.log(TodoList.$props) console.log(TodoList.$parent) console.log(TodoList.$children) console.log(TodoList.$refs) console.log(TodoList.$scopedSlots) console.log(TodoList.$slots) console.log(TodoList.$listeners) console.log(TodoList.$attrs) console.log(vm.$isServer) </script> </body>
</html>
|