diff --git a/vue.md b/vue.md index 5b3706091..c71d8d262 100644 --- a/vue.md +++ b/vue.md @@ -2,116 +2,362 @@ title: Vue.js category: JavaScript layout: 2017/sheet -updated: 2019-11-22 +updated: 2019-12-26 weight: -10 intro: | [Vue.js](https://vuejs.org/) is an open-source Model–view–viewmodel JavaScript framework for building user interfaces and single-page applications. --- -{% raw %} +{%raw%} -### Lists +Expressions +---------- +{: .-three-column} + +### Expressions ```html -
  • - {{ todo.text }} - {{ $index }} +
    +

    I have a {{ product }}

    +

    {{ product + 's' }}

    +

    {{ isWorking ? 'YES' : 'NO' }}

    +

    {{ product.getSalePrice() }}

    +
    +``` + +See: [Delimiters](https://vuejs.org/v2/api/#delimiters) + +### Binding + +```html +... +``` + +#### Shorthand syntax +```html +... +``` +{: data-line="1"} + +#### True or false will add or remove attribute +```html + +
  • ... ``` -### Components - -```js -new Vue({ - components: { app: App } -}) +#### To iterate through objects +```html +
  • ... ``` -## API - -```js -Vue.extend({ ... }) // creating components -Vue.nextTick(() => {...}) - -Vue.set(object, key, val) // reactive -Vue.delete(object, key) - -Vue.directive('my-dir', { bind, update, unbind }) -//
    - -Vue.elementDirective('my-dir', { bind, update, unbind }) -// ... - -Vue.component('my-component', Vue.extend({ .. })) - -Vue.partial('my-partial', '
    hi {{msg}}
    ') -// +#### Using `v-for` with a component +```html + ``` -```js -new Vue({ - data: { ... } - props: ['size'], - props: { size: Number }, - computed: { fullname() { return this.name + ' ' + this.lastName } }, - methods: { go() { ... } }, - watch: { a (val, oldVal) { ... } }, - el: '#foo', - template: '...', - replace: true, // replace element (default true) +See: [List Rendering](https://vuejs.org/v2/guide/list.html) - // lifecycle - created () {}, - beforeCompile () {}, - compiled () {}, - ready () {}, // $el is inserted for the first time - attached () {}, - detached () {}, - beforeDestroy () {}, - destroyed () {}, - // options - directives: {}, - elementDirectives: {}, - filters: {}, - components: {}, - transitions: {}, - partials: {} -}) -``` +Component +-------- -## Vue templates -Via [vueify](https://www.npmjs.com/package/vueify) +### Component anatomy ```js -// app.vue - - - + }, + data: function() { + // `data` must be a function + return { + firstName: 'Vue', + lastName: 'Mastery' + } + }, + computed: { + // Return cached values until dependencies change + fullName: function () { + return this.firstName + ' ' + this.lastName + } + }, + watch: { + // Called when firstName changes value + firstName: function (value, oldValue) { ... } + }, + methods: { ... }, + template: '{{ message }}', + // Can also use backticks in `template` for multi-line +}) ``` +{: data-line="3, 8, 16, 21, 28, 34, 39"} -Also +See: [Components Basics](https://vuejs.org/v2/guide/components.html) +### Lifecycle hooks + +| Method | Description | +| --- | --- | +| `beforeCreate` | After the instance has been initialized [#](https://vuejs.org/v2/api/#beforeCreate) | +| `created` | After the instance is created [#](https://vuejs.org/v2/api/#created) | +| `beforeMount` | Before the first render [#](https://vuejs.org/v2/api/#beforeMount) | +| `mounted` | After the instance has been mounted [#](https://vuejs.org/v2/api/#mounted) | +| `beforeUpdate` | When data changes, before the DOM is patched [#](https://vuejs.org/v2/api/#beforeUpdate) | +| `updated` | After a data change [#](https://vuejs.org/v2/api/#updated) | +| `beforeDestroy` | Before the instance is destroyed [#](https://vuejs.org/v2/api/#beforeDestroy) | +| `destroyed` | After a Vue instance has been destroyed [#](https://vuejs.org/v2/api/#destroyed) | + +See: [Lifecycle Hooks](https://vuejs.org/v2/api/#Options-Lifecycle-Hooks) + +### Custom events + +#### Set listener on component, within its parent ```html - + ``` -{% endraw %} +#### Inside parent component +```js +methods: { + incWithVal: function (toAdd) { ... } +} +``` + +#### Inside button-counter template +```js +this.$emit( + 'incrementBy', // Custom event name + 5 // Data sent up to parent + ) +``` + +Use props to pass data into child components, +custom events to pass data to parent elements. + +See: [Custom Events](https://vuejs.org/v2/guide/components-custom-events.html) + +Single file components +-------- + +### Single file +```html + + + + + +``` + +See: [Single File Components](https://vuejs.org/v2/guide/single-file-components.html) + +### Separation +```html + + + +``` + +See: [What About Separation of Concerns?](https://vuejs.org/v2/guide/single-file-components.html#What-About-Separation-of-Concerns) + +Slots +-------- + +### Using a single slot + +#### Component template +```html +
    +

    I'm a title

    + + Only displayed if no slot content + +
    +``` +{: data-line="3,4,5"} + +#### Use of component with data for slot +```html + +

    This will go in the slot

    +
    +``` +{: data-line="2"} + +See: [Slots](https://vuejs.org/v2/guide/components-slots.html) + +### Multiple slots + +#### Component template +```html +
    +
    + +
    +
    + Default content +
    +
    + +
    +
    +``` +{: data-line="3,6,9"} + +#### Use of component with data for slots +```html + +

    Page title

    +

    the main content.

    +

    Contact info

    +
    +``` +{: data-line="2,3,4"} + +See: [Slots](https://vuejs.org/v2/guide/components-slots.html) + +Also see +-------- + +* [Vue CLI](https://cli.vuejs.org/) _(cli.vuejs.org)_ +* [Vue Router](https://router.vuejs.org/) _(router.vuejs.org)_ +* [Vue DevTools](https://chrome.google.com/webstore/detail/vuejs-devtools/nhdogjmejiglipccpnnnanhbledajbpd?hl=en) _(chrome.google.com)_ +* [Nuxt.js](https://nuxtjs.org/) _(nuxtjs.org)_ +* [Vue.js v1.0.28 cheatsheet](vue@1.0.28/) _Legacy version_ + +{%endraw%} diff --git a/vue@1.0.28.md b/vue@1.0.28.md new file mode 100644 index 000000000..a6267d87a --- /dev/null +++ b/vue@1.0.28.md @@ -0,0 +1,117 @@ +--- +title: Vue.js v1.0.28 +category: JavaScript +layout: 2017/sheet +deprecated: true +weight: -10 +intro: | + **Deprecated:** this guide targets an old version of Vuej.js (v1.0.28). See the [updated Vue.js cheatsheet](vue) for new versions. +--- + +{% raw %} + +### Lists + +```html +
  • + {{ todo.text }} + {{ $index }} +
  • +``` + +### Events + +```html + +``` + +### Components + +```js +new Vue({ + components: { app: App } +}) +``` + +## API + +```js +Vue.extend({ ... }) // creating components +Vue.nextTick(() => {...}) + +Vue.set(object, key, val) // reactive +Vue.delete(object, key) + +Vue.directive('my-dir', { bind, update, unbind }) +//
    + +Vue.elementDirective('my-dir', { bind, update, unbind }) +// ... + +Vue.component('my-component', Vue.extend({ .. })) + +Vue.partial('my-partial', '
    hi {{msg}}
    ') +// +``` + +```js +new Vue({ + data: { ... } + props: ['size'], + props: { size: Number }, + computed: { fullname() { return this.name + ' ' + this.lastName } }, + methods: { go() { ... } }, + watch: { a (val, oldVal) { ... } }, + el: '#foo', + template: '...', + replace: true, // replace element (default true) + + // lifecycle + created () {}, + beforeCompile () {}, + compiled () {}, + ready () {}, // $el is inserted for the first time + attached () {}, + detached () {}, + beforeDestroy () {}, + destroyed () {}, + + // options + directives: {}, + elementDirectives: {}, + filters: {}, + components: {}, + transitions: {}, + partials: {} +}) +``` + +## Vue templates +Via [vueify](https://www.npmjs.com/package/vueify) + +```js +// app.vue + + + +``` + +Also + +```html + +``` + +{% endraw %}