Category Archives: Javasript

Error message of V-IF and V-for mutually exclusive codes in Vue project

Never use V-IF and V-for on the same element at the same time.

<!-- bad: control error-->
<ul>
  <li
    v-for="user in users"
    v-if="shouldShowUsers"
    :key="user.id">
      {{ user.name }}
  </li>
</ul>

We tend to do this in two common situations:

To filter items in a list (such as V-for = “user in users” V-IF = “user.Isactive”). In this case, replace users with a calculated attribute (such as activeusers) to return the filtered list.

computed: {
  activeUsers: function () {
    return this.users.filter((user) => {
      return user.isActive
    })
  }
}


<ul>
  <li
    v-for="user in activeUsers"
    :key="user.id">
      {{ user.name }}
  </li>
</ul>

To avoid rendering lists that should have been hidden (such as V-for = “user in users” V-IF = “shouldshowusers”). In this case, move the V-IF to the container element (such as UL, OL).

<!-- bad -->
<ul>
  <li
    v-for="user in users"
    v-if="shouldShowUsers"
    :key="user.id">
      {{ user.name }}
  </li>
</ul>

<!-- good -->
<ul v-if="shouldShowUsers">
  <li
    v-for="user in users"
    :key="user.id">
      {{ user.name }}
  </li>
</ul>

How to Solve JavaScript Error: replaceAll is not a function

Why do the above error reports occur

If you see the error “typeerror: replaceall is not a function”, it may be that your browser version or node.js version does not support this method.

we should note that: string. Prototype. Replaceall()   The method is added in ES2021/ES12, which is probably not supported by the environment.

How to solve it?

You can use “string. Prototype. Replace()” as a substitute for “string. Prototype. Replaceall()”, replace()   Just set a global (g) in the replaced regular expression. This processing method is the same as that of replaceall(). Here is an example of comparison:

const str = 'foo-bar';

// in older browsers
const result1 = str.replace(/foo/g, 'moo');

// ES12+
const result2 = str.replaceAll('foo', 'moo');

// output: 'moo-bar'
console.log(result1);
console.log(result2);

[Solved] Vue router common problems (push error, push duplicate route refresh)

Vue router FAQ

It is used to record Vue router bugs and common solutions encountered in work

Router.push reports an error, avoided redundant navigation to current location: “/xxx”

The main idea is that frequent route clicks lead to repeated routes. This error has no impact on the route jump function

Solution: override the push method

If you capture the exception, you will not report an error

let routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(err => err)
}

Click the route repeatedly to refresh the page effect

When rewriting the vuerouter.prototype.push method, compare the fullpath attribute in the currentroute object of vuerouter with the parameters of the push function to judge whether it is the same route. If yes, use the following method to complete the page refresh

Method 1: window. Location. Reload()

The problem is that it will refresh the whole page, which is not recommended

let routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {

  let history = this.currentRoute && this.currentRoute.fullPath;
  if (location === history) {
    window.location.reload();
  }
  return routerPush.call(this, location).catch(err => err)
}

Method 2: V-IF controls router view to realize local refresh

More complex, but can achieve local refresh

Since this in VueRouter.prototype.push points to the VueRouter instance
The reference property "app" (this.app) of the "global Vue instance" (generated by main.js new Vue()) exists in that instance

So, the push function can manipulate the Vue instance inside.

Realize thinking:

  The vue instance uses provide to inject a getRouterAlive method, which returns the state variable isRouterAlive defined in the current vue instance.
    Then define a reloadChild method to change the state variable isRouterAlive to false, and then change it to true after the dom update

  Where a local refresh is needed, inject accepts the method getRouterAlive and binds it to the router-view
    v-if="Object.prototype.toString.call(getRouterAlive) == '[object Function]' && getRouterAlive()"

Implementation code:

main.js

// main.js

new Vue({
  router,
  store,
  render: h => h(App),
  provide() {
    return {
      getRouterAlive: this.getRouterAlive,
    }
  },
  data: {
    isRouterAlive: true
  },
  methods: {
    getRouterAlive() {
      return this.isRouterAlive;
    },
    reloadChild() {
      this.isRouterAlive = false;

      this.$nextTick(() => {
        this.isRouterAlive = true;
      })
    }
  }
}).$mount('#app')

router.js

// router/index.js
let routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {

  let history = this.currentRoute && this.currentRoute.fullPath;
  if (location === history) {
    this.app.reloadChild && this.app.reloadChild();
  }
  return routerPush.call(this, location).catch(err => err)
}

Local refresh


...
<router-view v-if="Object.prototype.toString.call(getRouterAlive) == '[object Function]' && getRouterAlive()"></router-view>
...

inject: ["getRouterAlive"],
...

NPM Install vue-cli Error: internal/modules/cjs/loader.js (Error: Cannot find module ‘D:\Program\nodejs\node_global\node_modules\vue-cli\bin\vue’)

1. Vue cli installation encountered a problem

Install the new version of Vue cli command: NPM install – G @Vue/cli. After successful installation, use Vue – V to check the version number. An error is found:

PS D:\Program\nodejs\node_global> vue -V
internal/modules/cjs/loader.js:583
    throw err;
    ^

Error: Cannot find module 'D:\Program\nodejs\node_global\node_modules\vue-cli\bin\vue'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
    at Function.Module._load (internal/modules/cjs/loader.js:507:25)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
PS D:\Program\nodejs\node_global> npm -v
6.4.1

This error occurs when checking the Vue version or creating a new Vue project. Uninstalling Vue CLI and reinstalling will also report an error.

2. Solution

1. Find the installation location of your Vue first (you need to view the global location of your NPM installation)

My is installed in this directory. You can see that there will be two files. When there is a problem, the vue.cmd file is the latest (today’s), but the Vue file is 2020 (before). Therefore, it is speculated that the Vue versions of the two files may be inconsistent.

2. Get the location of the Vue file and delete it

Delete the remaining Vue files (delete these 2 files)

3. Uninstall Vue CLI and then reinstall it.

PS D:\Program\nodejs\node_global> vue -V
@vue/cli 4.5.13

This is successful.

Summary:

1. You can install without an administrator, but CMD should be run as an administrator when uninstalling;

2. Be calm when you encounter problems and remember to find the cause of the problem according to the error information. (there are too many Baidu search solutions for environmental problems, but many of them are not necessarily the same as the problems you encounter. Some others can solve them, but your problems may not be solved. It may lead to some other pits, so be careful.)

Vue Error: Error in render: “TypeError: Cannot read properties of null (reading ‘xxx’)” found in

Front end Vue error [Vue warn]: error in render: “typeerror: cannot read properties of null (reading ‘name’)” found in

This error is that the type with null attribute cannot be loaded. I used a stupid method to mark all null values on the page with Ctrl + F, and then modify them to 0 (0 is convenient for writing). Then I found that the error of the current page has not changed. Finally, I located the error in the subcomponent I called, and I called a pop-up window

When I delete this reference, I will not report an error, that is, an error occurs when I reference
I use a stupid method to locate and change the values of all null attributes to those with values. At this time, my error prompt changes
finally, it is located that the attribute I received is changed from nulll to [], which is also a problem caused by my carelessness

I hope I can help you

Vue Error: type check failed for prop “xxx“. Expected String with value “xx“,got Number with value ‘xx‘

Vue error reporting [Vue warn]: Invalid prop: type check failed for prop “name”. Expected String with value “4”, got Number with value 4.

When this error occurs, the reason is that the type relationship between number and string is omitted

It refers to arrays and variables or functions defined by us

The problem is that we click: the default type of name is string type, while we define the numeric type ourselves, so we need to change it

I hope the above can help you

[Solved] Vue Error: Property or method “changeLoginType” is not defined on the instance but referenced during render

reason

I’m here because the method in my code doesn’t exist. I missed it and added it later

Solution

Add the following code in methods:

// Modify Login Status
changeLoginType(bool){
   this.loginType = bool
}