Encapsulating a public method is a necessary first step: Encapsulating a public method 1. Encapsulating a public method 1 is a necessary first step in a business where the same method needs to be called multiple times and the code is not elegant enough to write it every time. SRC /assets/js/ create a js file in the Vue project: common.js 2. References common-> in main.js and instantiates it

/* public function */ import common from './assets/js/common' vue.use (common);

3.common.js Write an example

Export default{install(Vue){getTime = function(){var Date = new; var Date = new Date() var y = date.getFullYear() var m = date.getMonth() + 1 m = m < 10 ? ('0' + m) : m var d = date.getDate() d = d < 10 ? ('0' + d) : d alert(y + m + d) } } }

Here we have the sample public method encapsulated, and then how do we display the call

1. The life cycle of this in any Vue file. The method name is fine, because it’s already globally instantiated in main.js

created(){
   this.getTime() 
}

2. The page will call the method