diff --git a/docs/vue2.md b/docs/vue2.md index 6e5f47ef..08d02c0a 100644 --- a/docs/vue2.md +++ b/docs/vue2.md @@ -12,6 +12,7 @@ Vue 是一套用于构建用户界面的渐进式框架 - [Vue 2.x 官方文档](https://v2.cn.vuejs.org/) - [Vue Router 3.x 官方文档](https://v3.router.vuejs.org/) + #### 快速创建 **Vue** 项目 ([Vue CLI](https://cli.vuejs.org/zh/guide/creating-a-project.html)) @@ -27,6 +28,8 @@ npx @vue/cli create hello-world ``` +--- + ```js var app = new Vue({ el: '#app', @@ -48,6 +51,8 @@ var app = new Vue({ ``` +--- + ```js var vm = new Vue({ el: '#example', @@ -82,6 +87,8 @@ var vm = new Vue({ ``` +--- + ```js var app2 = new Vue({ el: '#app-2', @@ -100,6 +107,8 @@ var app2 = new Vue({ ``` +--- + ```js var app3 = new Vue({ el: '#app-3', @@ -123,6 +132,8 @@ var app3 = new Vue({ ``` +--- + ```js var app4 = new Vue({ el: '#app-4', @@ -147,6 +158,8 @@ var app4 = new Vue({ ``` +--- + ```js var app5 = new Vue({ el: '#app-5', @@ -706,8 +719,11 @@ var example2 = new Vue({ } } }) +``` -// 也可以用 JavaScript 直接调用方法 +也可以用 JavaScript 直接调用方法 + +```js example2.greet() // => 'Hello Vue.js!' ``` @@ -784,7 +800,9 @@ methods: { -

...

+

+ ... +

``` 这个 `.passive` [修饰符](#v-on-事件修饰符)尤其能够提升移动端的性能。 @@ -802,7 +820,6 @@ methods: {
``` - ### .exact 修饰符 ```html @@ -814,6 +831,651 @@ methods: { + ` +}) +``` + +组件是可复用的 `Vue` 实例 + +```html +
+ +
+``` + +--- + +```js +new Vue({ + el: '#components-demo' +}) +``` + +组件的复用 + +```html +
+ + + +
+``` + +### `data` 必须是一个函数 + +```js +data: function () { + return { + count: 0 + } +} +``` + +组件的 `data` 选项必须是一个函数 + +### 向子组件传递数据 + +```js +Vue.component('blog-post', { + props: ['title'], + template: '

{{ title }}

' +}) +``` + +当值传递给一个 `prop` `attribute` 的时候,变成了组件实例的一个 `property` + +```html + + +``` + +### 单个根元素 + +```js {4} +Vue.component('blog-post', { + props: ['post'], + template: ` +
+

{{ post.title }}

+
+
+ ` +}) +``` + +--- + +```html + + +``` + +### 监听子组件事件 + + +```js {7} +Vue.component('blog-post', { + props: ['post'], + template: ` +
+

{{ post.title }}

+ +
+
+ ` +}) +``` + +--- + +```html {3} + +``` + +可以使用 `$emit` 的第二个参数来提供这个值 + +```html {2} + +``` + +通过 `$event` 访问到被抛出的这个值 + +```html {3} + +``` + +如果这个事件处理函数是一个方法 + +```html {3} + +``` + +那么这个值将会作为第一个参数传入这个方法 + +```js +methods: { + onEnlargeText: function(enlargeAmount) { + this.postFontSize += enlargeAmount + } +} +``` + +### 在组件上使用 v-model + + +```html + +``` + +等价于 + +```html + +``` + +为了让它正常工作,这个组件内的 \ 必须: + +- 将其 `value` attribute 绑定到一个名叫 `value` 的 `prop` 上 +- 在其 `input` 事件被触发时,将新的值通过自定义的 `input` 事件抛出 + +--- + +```js +Vue.component('custom-input', { + props: ['value'], + template: ` + + ` +}) +``` + +现在 `v-model` 就应该可以在这个组件上完美地工作起来了 + +```html {2} + + +``` + +### 通过插槽分发内容 + +```html + + 发生了不好的事情。 + +``` + +--- + +```js {5} +Vue.component('alert-box', { + template: ` +
+ Error! + +
+ ` +}) +``` + +### 动态组件示例 + + +```html +
+ + +
+ + +``` + +### 解析 DOM 模板时的注意事项 + +有些 HTML 元素,诸如 `