在vue-ssr项目中渲染一个vue实例,直接像spa那样初始化vue实例的话事件不会被触发,想知道怎么给template中的dom元素绑定事件?
相关代码
const fs = require('fs')
const Vue = require('vue')
const server = require('express')()
const VueServerRenderer = require('vue-server-renderer')
const renderer = VueServerRenderer.createRenderer({
template: fs.readFileSync('./index.template.html', 'utf-8')
})
server.get('*', (req, res) => {
const app = new Vue({
template: `
<div>
<button @click="testFun">click me</button>
<br/>
</div>
`,
methods: {
testFun() {
console.log('----------------')
}
}
})
renderer.renderToString(app, context).then(html => {
res.end(`${html}`)
}).catch(err => {
res.status(500).end('Internal Server Error')
return
})
})
server.set('port', process.env.PORT || 6666)
let hostname = '127.0.0.1'
server.listen(server.get('port'), hostname, () => {
console.log(`Server running at http://${hostname}:${server.get('port')}`)
})
希望知道怎么在ssr中写这些函数,谢谢