• 赚钱入口【需求资源】限时招募流量主、渠道主,站长合作;【合作模式】CPS长期分成,一次推广永久有收益。主动打款,不扣量;

Uncaught TypeError: Cannot read property ‘settings’ of undefined

Vue cps12345 1年前 (2020-08-09) 404次浏览 0个评论

vue如下:

<template>
    <div>
        <div class="inner_container">
          <p>
            URL:
            <span>{{settings.url}}</span>
          </p>
          <div class="input_row">
            <input
              class="input_text"
              id="getURL"
              type="url"
              inputmode="url"
              placeholder="Enter URL"
              title="URL"
              @input="changeUrl"
            />
            <label class="label_" for="getURL">URL Link</label>
          </div>
          <button class="button" @click="saveSettings">Save all Values</button>
        </div>
    <div>
</template>

<script>
import axios from "axios";
import _ from "lodash";

export default {
  name: "Home",
  data() {
    return {
      settings: {
        brightness: "",
      },
    };
  },
  methods: {
    changeUrl: _.debounce((e) => {
      this.settings.url = e.target.value;
    }, 500),
  },
};
</script>
报错:
Uncaught TypeError: Cannot read property ‘settings’ of undefined

原因

问题在于this没有引用您想要的对象。一种解决方案是使用普通函数并url在settings以下项中预定义属性:

new Vue({
     el: '#app',
     data() {
          return {
               settings: {
                    brightness: "",
                    url: ""
               },
          };
     },
     methods: {
          changeUrl: _.debounce(function(e) { 
               this.settings.url = e.target.value; 
          }, 500),
          saveSettings(){
          
          }
     },
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.19/lodash.min.js"></script>
<script src="https://unpkg.com/vue/dist/vue.min.js"></script>

<div id="app">
     <div class="inner_container">
          <p>
               URL: <span>{{settings.url}}</span>
          </p>
          <div class="input_row">
               <input
                    class="input_text"
                    id="getURL"
                    type="url"
                    inputmode="url"
                    placeholder="Enter URL"
                    title="URL"
                    @input="changeUrl"
               />
               <label class="label_" for="getURL">URL Link</label>
          </div>
          <button class="button" @click="saveSettings">Save all Values</button>
     </div>
</div>

 

喜欢 (0)

您必须 登录 才能发表评论!