Vue global components and local components, view the global components mounted by vue

How to create global references or global commands in Vue

1 Generally, in vue, there are many vue components, and each of these components is a file. may need to refer to the same module (or plugin). We don't want to import the module once per file.

If it is a plug-in based on writing, we can use (...)2, but what if we want to add a global command and make it available to every vue file at the same time?

Step 1: It is best to create a global command file For example: Step 2: Use Vue.directive() to create a global command and expose it, such as a focus to automatically focus the form Step 3: In (entry JS file), you can omit the file suffix, so that any Vue file can be easily used as long as the v-focus (command name) is in this way. 3Vue.directive() commands are generally automatically run or initialized What should I do if it is triggered by the wait and cannot be used for asynchronous events?

So we can use the 'mixins' mixed command, you'd better create a special folder for storing mixed commands, for example: saveScrollPosition (not saveScrollPosition in vue) can save the browsing position between routing jumps every time Information Note: In vue2.0, the location information will be automatically saved between routing jumps, but there are bugs (the location information will interfere with each other).

So after we write a new saveScrollPosition and expose it, it will be very convenient to mix it in the page you need.

4 If you need to apply a plug-in, and it is not written based on the plug-in command, then you can assign it to the prototype of Vue. For example: I want to refer to axios globally, we can do this and then this.$(url) and so on 5 Hang the required variables on the window object, such as: third-party libraries, etc. Note: This method is not suitable for server-side rendering, and the server does not have a window object Emphasis: all the above imports must be imported through the entry JS file , so that it can be applied globally.

Google AI Writing Project: Little Fat Cat

How to create global references or global commands in Vue

1 Generally, in vue, there are many vue components, each of which is a file typescript tutorial entry, typescript entry and actual combat . may need to refer to the same module (or plugin). We don't want to import the module once per file.

If it is a plug-in based on writing, we can use (...)2, but what if we want to add a global command and make it available to every vue file at the same time?

Step 1: It is best to create a global command file For example: Step 2: Use Vue.directive() to create a global command and expose it, such as a focus to automatically focus the form Step 3: In (entry JS file), you can omit the file suffix so that any Vue file can be easily used as long as v-focus (command name) is like this.

How to create global references or global commands in Vue

1 Generally, in vue, there are many vue components, and each of these components is a file. may need to refer to the same module (or plugin). We don't want to import the module once per file.

If it is a plug-in based on writing, we can use ()2, but what if we want to add a global command and make it available to every vue file at the same time?

Step 1: It is best to create a global command file For example: Step 2: Use Vue.directive() to create a global command and expose it, such as a focus to automatically focus the form Step 3: In (entry JS file), you can omit the file suffix so that any Vue file can be easily used as long as v-focus (command name) is like this.

How does vue get globally registered components

Introduce the component at the beginning, and then register the component, for example: importVuefrom'vue'importVueRouterfrom'vue-router'importVueResourcefrom'vue-resource'importSixiButtonfrom'components/common/SixiButton'(VueRouter)(VueResource)Vue.component('six-button' , SixiButton).

How does vue component use global variables

The principle of defining global variables: set up a dedicated global variable module file, define the initial state of some variables in the module, expose them with exportdefault, and use Vue. module.

Global variable module file: File: Usage 1: Reference the global variable module file where needed, and then obtain the global variable parameter value through the variable name in the file.

Use in components: Usage 2: In the program entry file, mount the above file to Vue.prototype.

importglobal_from'./components/Global'//Reference file Vue.prototype.GLOBAL=global_//Mounted to the Vue instance and then in the whole project, there is no need to refer to the module file, and you can directly access the Global file directly through this Defined global variables.

: Vuex can also set global variables: use vuex to store global variables, there are many things here, and it is relatively complicated. Interested friends can check the information by themselves, toss a wave, define the principles of global functions and create a new module file. Then mount the function to the Vue instance through Vue.prototype, and run the function through this.function name.

1. Write functions directly in it. Simple functions can be called directly in components written in it: 2. Write a module file and mount it on it.

The file and file location can be placed at the same level, which is convenient for referencing the entry file: call in the component: the following is how to define the content of the global variable global function. The global variable global function here is not limited to the vue project, vue-cli It uses webpack for modularization. For other modular developments, the routines for defining global variables and functions are basically the same.

How does vue reference components? Must rely on node's server environment

OK, understandable 2. The bottom navigation bar is a common component, so it’s OK to understand?

OK, it can be understood in this way 3. Now several pages have to refer to the components of this navigation bar OK, one of the purposes of the components is to reuse 4. How to quote, see some examples on git, all rely on npminstall, and then npmrun Ah, and then run the server to keep the port strong. Yes, it is usually taught on the Internet, simple and fast. 5. I don’t understand, can’t I do it if I don’t want to run a server?

sure. All kinds of npminstall and npmrun taught online are for running a local debugging server, which is convenient for development and debugging. And it doesn't matter what strategy is used for online deployment.

However, there are indeed many functions that rely on the capabilities provided by the server to run correctly (or to be consistent with online behavior), so starting a debugging server is still the best way.

For example: ajax, various front-end routing tools, if they cooperate with the server, the development and debugging effects will be better. node is here.

Vue custom global method, introduction to use in components

Global registration in Vue.prototype.funcName=function(){} can call this.funcName() in all components; the above Vue custom global method, the use introduction in the component is shared by the editor to everyone The whole content is here, I hope it can give you a reference, and I hope you can support developpaer a lot.

Articles you may be interested in: Talking about vue customizing global components and using the component through the global method () to customize the use of vue global components use, detailed explanations of the use of vuex, detailed explanations of customizing your own global components, global components and local components of Vue components Detailed explanation of the implementation method of custom global components in VueDetailed implementation of internal custom instructions and global custom instructions (using directive)

Vue sets a global variable, how to modify it in component use

Introduce importdefinesfrom'./config'Vue.prototype.defines=defines as follows: exportdefault{name:'Who',sex:'Male',setname(newName){=newName('name has changed')}} Then add: { {}} to the html of vue that calls the global variable to display the global variable.

The modification method is: add: this.defines.setname('Xiaoqiang')() to the vue script that calls the global variable to modify it.

Can mapGetters, mapMutations, mapActions be referenced globally in VUE?

 

Guess you like

Origin blog.csdn.net/mynote/article/details/126861464#comments_26879861