【发布时间】:2021-01-28 19:38:24
【问题描述】:
Laravel 版本:8.7.1
livewire 版本:2.x.x
您必须查看代码才能理解我的问题
此函数将返回一个用于渲染的模板
function createElementForAttatchment(tit,capt,id){
let template =
` <div class="approval-list-tab display-flex
flex-direction-column flex-align-item-start
flex-justify-content-space-between" wire:click="$emit('laucnhLeave',${id})">
<h1 style="text-align: left">${tit}</h1>
<h2>${capt}</h2>
</div>`
return template
}
下面的代码将从服务器获取数据并执行上面的函数
function getDataFromServer(){
axios.get(appUrl+"/api/attendences/for/approval").then(response=>{
console.log(response.data)
let data = response.data.data
nextPageUrl = response.data.next_page_url
data.forEach(d => {
let title = d.first_name + " " + d.last_name
let caption = "ROll NUMBER : "+d.roll_number + " Class : "+d.student_class_id
scollingElement.insertAdjacentHTML("beforeend",createElementForAttatchment(title,caption,d.id))
})
}).catch(error =>{
console.log(error)
})
}
现在wire:click 不工作了。但是我单独对其进行了测试,它运行良好,但在我使用 insertAdjacentHTML 时无法正常工作
点击事件监听的代码是:
Livewire.on('laucnhLeave', (id) => {
alert(id)
})
谢谢!
【问题讨论】:
-
有什么原因吗?
-
使用 javascript 添加
wire:click不起作用。不会有任何 javascript 事件链接到此按钮。wire:click应该添加到刀片中。这就是 Livewire 的工作方式,一切都在 Laravel 后端,而不是 JavaScript 前端。
标签: javascript laravel laravel-livewire