我有一個帶有前端的 vue js 專案,我有一個帶有變數的欄位,當它改變時我想要它,它會轉到后端并在那里改變任何想法?變數是此代碼中的 id 標記
app.get('/payments', (request, response) => {
response.set('Access-Control-Allow-Origin','*')
let payments = []
db.collection("payments").where("id", "==", idtag).get().then(snapshot => {
snapshot.forEach((doc) => {
console.log(doc.id, '=>', doc.data())
payments.push(doc.data())
})
response.send(payments)
console.log('payments:',payments)
})
})
這是前端
export default defineComponent({
setup () {
const loadinga = ref(false)
const idtag=ref(null)
const filter = ref('')
return {
events: [ '2019/02/01', '2019/02/05', '2019/02/06' ],
date : ref('2019-02-22 21:02'),
columns,
loadinga,
idtag,
uj5u.com熱心網友回復:
使 ID 成為請求 url 的一部分:
// backend assuming this is express
app.get('/payments/:id', (request, response) => {
// use the id in some way
const idtag = req.params.id;
...
});
在您的前端代碼中,您需要觀察idTag 并在每次更改時發起一個新請求。
// frontend
import { defineComponent, ref, watch } from "vue";
export default defineComponent({
setup() {
const idtag = ref(null);
watch(idtag, (newValue, oldValue) => {
fetch(`/payments/${newValue}`)
.then(raw => raw.json())
.then(data => {
// do something with the data
console.log(data)
})
.catch(console.warn);
});
return { idtag };
}
})
如果您需要立即運行,則應改用watchEffect。但是由于您的值一開始是 null ,所以我認為情況并非如此。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/474077.html
標籤:javascript 节点.js Vue.js Vuex 类星体
上一篇:即使在“回傳”之后回圈也不會終止
