简体   繁体   English

我想使用 Vue.js 进行实时加载

[英]I want make real time load using Vue.js

I want to retrieve my added new Quote without need of page reload.我想检索我添加的新报价而不需要重新加载页面。 Now I have to refresh page, then I will be able to see data.现在我必须刷新页面,然后我才能看到数据。 I am using restdb.io as a database, so after making post request, how can I retrieve all data without page reload, could you please give some advice, maybe try something else我正在使用restdb.io作为数据库,所以在发出post请求后,如何在不重新加载页面的情况下检索所有数据,请你给点建议,也许试试别的

Emitted method发射方法

methods: {
     addQuote(e) {
        e.preventDefault();
        if (this.text.length === 0) {
            alert("Поле пустое. Пожалуйста введите цитату");
            return;
        }
        const newQuote = this.text;
        this.$emit("add-quote", newQuote);
        this.text = "";
    }
}

POST request POST 请求

addQuote(quote) {
    if (this.quotes.length === 10) {
        alert("Для добавления новых цитат удалите одну из добавленных");
        return;
    }
    axios
        .post(
            "https://beeline-3fee.restdb.io/rest/db?apikey=<api_key>",
            { text: quote }
        )
        .then(response => response.data)
        .then(quote => {
            console.log("Success:", quote);
        })
        .catch(error => {
            console.error("Error:", error);
        });
    }
}

GET request获取请求

mounted() {
    axios
    .get(
        "https://beeline-3fee.restdb.io/rest/db?apikey=<api_key>"
    )
    .then(response => (this.quotes = response.data))
    .catch(err => console.log(err));
}

You must add a getQuotes method and use it to load the quotes in mounted and to fetch all the quotes after you add a new quote您必须添加一个getQuotes方法并使用它来加载已mounted的引号并在添加新引号获取所有引号

  mounted() {
    this.getQuotes();
  },
  methods: {
    getQuotes() {
      axios.get("https://beeline-3fee.restdb.io/rest/db?apikey=5eaaf516161b39295cdee783")
        .then((response) => (this.quotes = response.data))
        .catch((err) => console.log(err));
    },
    addQuote(quote) {
      if (this.quotes.length === 10) {
        alert("Для добавления новых цитат удалите одну из добавленных");
        return;
      }
      axios
        .post("https://beeline-3fee.restdb.io/rest/db?apikey=5eaaf516161b39295cdee783",
          {
            text: quote,
          }
        )
        .then((quote) => {
          console.log("Success:", quote);

          // this will fetch the quotes again
          this.getQuotes();
        })
        .catch((error) => {
          console.error("Error:", error);
        });
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM