Vue.js component script separation Author: Chuwen Time: 2021-10-14 Classification: Vue.js comment ##Required Usually, it is convenient to develop a single '. vue' file, but when the business logic is up, the 'script' and 'template' parts often need to be modified. It is not easy to use the editor to split the screen, so there is * * component script separation** ##Example ### `TableList.vue` ```vue <template> <div> <h1>Test {{test}}</h1> </div> </template> <script> import index from './index' export default index </script> ``` ### `index.js` >Create a file such as' index. js' at the same level of 'TableList. vue' ```js export default { name: "TableList", components: { 'pagination': Pagination }, data(){ return { test: 'test' } } } ```
Vue.js drop-down box component closes the menu when clicking outside the menu bar Author: Chuwen Time: 2021-08-23 Classification: Vue.js , JavaScript comment ## Menu.vue >Menu Components > ! []( https://cdn.nowtime.cc/2021/08/23/2193569241.png ) ``` <template> <div class="menu" v-if="show"> <!-- Suppose it is a menu component... --> </div> </template> <script> export default { data(){ return { Show: true,//Whether to display the menu } }, mounted() { //Just add it here document.addEventListener('click', (e) => { //If the DOM of the current component (menu component) "does not contain" the currently clicked DOM //Close the display menu if (! this.$el.contains(e.target)) this.show = false }) } } </script> ```
Vue.js uses the form and presses the Enter or Submit button to prevent the page from jumping Author: Chuwen Time: 2021-08-07 Classification: Vue.js , JavaScript 1 comment ####Add the '. event' event modifier to prevent jumping [ https://cn.vuejs.org/v2/guide/events.html# Event Modifier]( https://cn.vuejs.org/v2/guide/events.html# Event Modifier“ https://cn.vuejs.org/v2/guide/events.html# Event modifier ") >Another advantage of using 'form' is that you can submit the form by pressing Enter in input ```vue <!-- Add the. prevent modifier to prevent jumping --> <!-- Note that login is a user-defined function --> <form @submit.prevent="login"> <input type="text" /> <button type="submit">Submit </form> ```
Vue.js calls the Vue extension method of main.js in other js files Author: Chuwen Time: 2021-08-06 Classification: Vue.js , JavaScript comment >The description is not very accurate `Main. js ` The following methods are extended: ```js import Vue from 'vue' import App from './App.vue' import dayjs from 'dayjs' dayjs.locale('en') dayjs.extend(require('dayjs/plugin/localizedFormat')) //Look here! Vue.prototype.$dayjs = require('dayjs') new Vue({ render: h => h(App), }).$ mount('#app') ``` The 'Request. js' in a folder needs to call the method just extended. You need to do this: ```js //Be careful to import this package import Vue from 'vue' //So it can be used const dayjs = Vue.prototype.$dayjs console.log(dayjs()) ```
Vue.js error [Vue warn]: Computed property "getSearchOther" was assigned to but it has no setter Author: Chuwen Time: 2021-06-15 Classification: Vue.js comment ```log [Vue warn]: Computed property "getSearchOther" was assigned to but it has no setter. ``` I wrote: ```js //Some codes are omitted here watch:{ getSearchOther() { return this.SearchOther }, } //Some codes are omitted here ``` ##Solution Manually give 'get' and 'set' methods Refer to official documents:[ https://cn.vuejs.org/v2/guide/computed.html# Calculation property setter]( https://cn.vuejs.org/v2/guide/computed.html#%E8%AE%A1%E7%AE%97%E5%B1%9E%E6%80%A7%E7%9A%84 -setter " https://cn.vuejs.org/v2/guide/computed.html# Calculated property setter ") ```js //Some codes are omitted here watch:{ getSearchOther: { get() { return this.SearchOther }, set(newVal) { this.SearchOther = newVal; } }, } //Some codes are omitted here ```