Vue未捕获类型错误:fn.bind不是函数

jgzswidk  于 2023-05-23  发布在  Vue.js
关注(0)|答案(4)|浏览(112)

我的App.vue看起来如下

<template>
  <div id="app">
    <home-central></home-central>
  </div>

</template>

<script>
import HomeCentral from './components/HomeCentral';

export default {
  name: 'App',
  components: {
    HomeCentral,
  },
};
</script>
<style>
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

我在src/components/HomeCentral.vue中有以下代码

<template>
    <div class="homecentral">
        <input type="text" v-model="title"><br/>
        <h1>{{title}}</h1>
        <p v-if="showName">{{user.first_name}}</p>
        <p v-else>Nobody</p>
        <ul>
            <li v-for="item in items" :key="item.id">{{item.title}}</li>it
        </ul>
        <button v-on:click="greet('Hello World')">Say Greeting</button>
        <br/>
        <input type="text" v-on:keyup="pressKey" v-on:keyup.enter="enterHit">
        <label>First Name: </label><input type="text" v-model="user.firstName">
        <br/>
        <label>Last Name: </label><input type="text" v-model="user.lastName">
        <h3></h3>
    </div>
</template>

<script>
export default {
  name: 'HomeCentral',
  data() {
    return {
      title: 'Welcome',
      user: {
        firstName: 'John',
        lastName: 'Doe',
      },
      showName: true,
      items: [
          { title: 'Item One' },
          { title: 'Item Two' },
          { title: 'Item Three' },
      ],
    };
  },
  methods: {
    greet: function (greeting) {
      alert(greeting);
    },
    pressKey: function (e){
      console.log('pressed' + e.target.value);
    },
    enterHit() {
      console.log('You hit enter');
    },
    computed: {
      fullName: function() {
        return this.user.firstName + ' ' + this.user.lastName;
      }
    },
  },
};
</script>

<style scoped>

</style>

这将引发以下错误:

vue.runtime.esm.js?ff9b:205 Uncaught TypeError: fn.bind is not a function
    at nativeBind (vue.runtime.esm.js?ff9b:205)
    at initMethods (vue.runtime.esm.js?ff9b:3537)
    at initState (vue.runtime.esm.js?ff9b:3305)
    at VueComponent.Vue._init (vue.runtime.esm.js?ff9b:4624)
    at new VueComponent (vue.runtime.esm.js?ff9b:4794)
    at createComponentInstanceForVnode (vue.runtime.esm.js?ff9b:4306)
    at init (vue.runtime.esm.js?ff9b:4127)
    at createComponent (vue.runtime.esm.js?ff9b:5604)
    at createElm (vue.runtime.esm.js?ff9b:5551)
    at createChildren (vue.runtime.esm.js?ff9b:5678)

如果我删除计算块,事情就会开始正常工作:

computed: {
  fullName: function() {
    return this.user.firstName + ' ' + this.user.lastName;
  }
},

请帮我找出我做错了什么。

oipij1gg

oipij1gg1#

方法块应该只包含JavaScript函数。当我有一个嵌套的对象,在方法块中有方法时,我也得到了这个错误。
即:

methods: {
  namespace: {
    methodName () {
    }
  }
}

应格式化为

methods: {
  namespace-methodName () {
  }
}
eivnm1vs

eivnm1vs2#

请添加下面的代码并尝试,

<template>
<div class="homecentral">
    <input type="text" v-model="title"><br/>
    <h1>{{title}}</h1>
    <p v-if="showName">{{user.first_name}}</p>
    <p v-else>Nobody</p>
    <ul>
        <li v-for="item in items" :key="item.id">{{item.title}}</li>it
    </ul>
    <button v-on:click="greet('Hello World')">Say Greeting</button>
    <br/>
    <input type="text" v-on:keyup="pressKey" v-on:keyup.enter="enterHit">
    <label>First Name: </label><input type="text" v-model="user.firstName">
    <br/>
    <label>Last Name: </label><input type="text" v-model="user.lastName">
    <h3></h3>
</div>
<script>
export default {
name: 'HomeCentral',
data() {
return {
  title: 'Welcome',
  user: {
    firstName: 'John',
    lastName: 'Doe',
  },
  showName: true,
  items: [
      { title: 'Item One' },
      { title: 'Item Two' },
      { title: 'Item Three' },
  ],
};
},
methods: {
greet: function (greeting) {
  alert(greeting);
},
pressKey: function (e){
  console.log('pressed' + e.target.value);
},
enterHit() {
  console.log('You hit enter');
}
},
computed: {
  fullName: function() {
    return this.user.firstName + ' ' + this.user.lastName;
  }    
},
};
</script>

<style scoped>

</style>

你不小心在方法内部嵌套了计算机。

2lpgd968

2lpgd9683#

vue namespace bug &解决方案

我永远不会推荐以这种方式使用vue和vue组件

点击事件绑定bug

解决方案(全局this命名空间bug)

只要返回一个vue示例,现在一切都好了。

更多详情
https://github.com/xgqfrms/vue/issues/49

lzfw57am

lzfw57am4#

我有

created: {

}

而它本该是

created() {

}

相关问题