The main content of this article is the usage of Vue in uni-app, as follows: Vue supports responsive data operations, It can realize the binding of data and events, and supports this transfer; uni-app adds application life cycle and page life cycle based on the Vue instance life cycle; 3 ways to implement global variables, namely Public modules, mounting Vue.prototype and globalData; Dynamic binding of Class and Style, including the use of object syntax and list syntax.
1. Use of properties and methods
Vue is a progressive framework based on JavaScript for building user interfaces, supportingResponsive data operation, after declaring a variable, the view will be re-rendered after the value of the variable changes, without the need to manually update the DOM node.
As you can see, all page file suffixes are .vue, which is a single file. The variables are declared in the data attribute of the script module. templateWhen using variables in a block, you need to include the variable with {<!-- -->{}}; You can also define methods to perform specific functions, which need to be included in In the methods attribute of the script module, events can be bound in the component at the same time. The format is v-on:click="event name" and @click="event name", corresponding events can be triggered based on conditions. For the correspondence between web events and events in uni-app, please refer to https://uniapp.dcloud.io/use?id=Event Processor.
Among them, this represents the Vue instance itself. In addition to calling properties, you can also call methods.
Display:
You can see that the name attribute has been changed.
If there are multiple functions nested in the function, there may be problems in passing this. In this case, you can use variables to replace , as follows:
You can see that after clicking the button, the name is changed first, and then changed again after 2 seconds, which means _self replaces this successfully.
2. Vue life cycle
Vue supports instance life cycle, uni-app adds application life cycle on this basis and Page life cycle.
Vue instance life cycle hook automatically binds this context to the instance, so you can access data and perform operations on properties and methods. details as follows:
Function
Meaning
##beforeCreate
After the instance is initialized, Data observer (data observer) and event/watcher event configuration are called before
created
is called immediately after the instance is created. At this step, the instance has completed the following configuration: data observer, property and method operations, and watch/event event callbacks. However, the mount phase has not started yet and the $el property is not yet available.
beforeMount
Called before the mount starts: the related render function is called for the first time.
mounted
Called after the instance is mounted, then el is replaced by the newly created vm.$el
beforeUpdate
Called when the data is updated, which occurs before the virtual DOM is patched. This is suitable for accessing the existing DOM before updating, such as manually removing an added event listener.
updated
The virtual DOM is re-rendered and patched due to data changes, after which this hook will be called
activated
Called when a component cached by keep-alive is activated
deactivated
Called when a component cached by keep-alive is deactivated
beforeDestroy
Called before the instance is destroyed
destroyed
Called after the instance is destroyed. After this hook is called, all instructions corresponding to the Vue instance are unbound, all event listeners are removed, and all child instances are destroyed.
errorCaptured
Called when capturing an error from a descendant component. This hook receives three parameters: the error object, the component instance where the error occurred, and a string containing information about the source of the error. This hook can return false to prevent the error from propagating further upwards.
The application life cycle belongs to the entire uni-app project and can only be monitored in App.vue. Monitoring on other pages is invalid.
The details are as follows:
Function
Meaning
onLaunch
Triggered when uni-app initialization is completed (only triggered once globally)
onShow
When uni-app starts, or enters the foreground display from the background
onHide
When uni-app enters the background from the front desk
onError
When uni-app reports an error Triggered when
onUniNViewMessage
To monitor the data sent by the nvue page, please refer to nvue communicating with vue
onUnhandledRejection
Listening function for unhandled Promise rejection event (2.8.1)
onPageNotFound
There is no listening function on the page
onThemeChange
Monitoring system theme changes
The page life cycle belongs to a specific page and is valid for the current page.
Common page life cycles are as follows:
Function
Meaning
onLoad
monitors page loading, its parameter is the data passed on the previous page, and the parameter type is Object (used for page parameters), refer to the example
onShow
Listen to the page display. Triggered every time the page appears on the screen, including returning from the lower-level page point to reveal the current page
onReady
Listens for the completion of the initial rendering of the page. Note that if the rendering speed is fast,
#onHide
will be triggered before the page entry animation is completed
onUnload
Listen for page unloading
onResize
Listen for window size changes
onPullDownRefresh
Monitor the user's pull-down action, generally used for pull-down refresh, refer to the example
onReachBottom
The event when the page scrolls to the bottom (not scroll-view to the bottom), often used for pull-down Next page of data. See the notes below for details
onPageScroll
Monitor page scrolling, the parameter is Object
onNavigationBarButtonTap
Listen to the native title bar button click event, the parameter is Object
三、全局变量
uni-app中实现全局变量有几种实现方式。
1.公用模块
定义一个专用的模块,用来组织和管理这些全局的变量,同时将它们作为常量,在需要的页面引入,如果这些常量需要改变,直接在模块中改变即可,而不需要再在每一个导入的页面手动修改,优化了项目的结构。 例如,在 uni-app 项目根目录下创建 common 目录,然后在 common 目录下新建 constants.js 用于保存公共的变量和方法,一般为常量,很少需要改动,如下:
The above is the detailed content of Learn to use Vue in uni-app. For more information, please follow other related articles on the PHP Chinese website!
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.
You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.
There are three ways to refer to JS files in Vue.js: directly specify the path using the <script> tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.
The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.
Vue multi-page development is a way to build applications using the Vue.js framework, where the application is divided into separate pages: Code Maintenance: Splitting the application into multiple pages can make the code easier to manage and maintain. Modularity: Each page can be used as a separate module for easy reuse and replacement. Simple routing: Navigation between pages can be managed through simple routing configuration. SEO Optimization: Each page has its own URL, which helps SEO.
Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses <router-link to="/" component window.history.back(), and the method selection depends on the scene.
You can query the Vue version by using Vue Devtools to view the Vue tab in the browser's console. Use npm to run the "npm list -g vue" command. Find the Vue item in the "dependencies" object of the package.json file. For Vue CLI projects, run the "vue --version" command. Check the version information in the <script> tag in the HTML file that refers to the Vue file.
Function interception in Vue is a technique used to limit the number of times a function is called within a specified time period and prevent performance problems. The implementation method is: import the lodash library: import { debounce } from 'lodash'; Use the debounce function to create an intercept function: const debouncedFunction = debounce(() => { / Logical / }, 500); Call the intercept function, and the control function is called at most once in 500 milliseconds.