I'm trying to print variable in the console, For this I have the configuration below. the first method works fine, but not the second method containing an argument.
In index.wxml
file, I have:
<button onclick='test1'>Button 1</button>
<button onclick='test2("bar")'>Button 1</button>
In index.js
, I have:
Page({
test1 {
console.log('foo')
},
test2(v) {
console.log('foo: ' + v)
},
})
What's wrong with the second method ?
I know that it's been a while since you asked this question and maybe you don't need an answer anymore, but I ran into this problem too, so probably others will too.
Page
is a function that takes a json object as input. So you should rewrite index.js
like this:
Page({
test1 : function() {
console.log('foo')
},
test2 : function(v) {
console.log('foo: ' + v)
},
})
The documentation for the Page
class is hard to find, and as far as I can tell, they haven't translated it. It exists here in Chinese.
Even though those are technically anonymous functions, you can use the keys of the json object in your wxml file as if they were functions. Your wxml file is basically right, but I'm not sure that onclick
actually works. Try bindtap
or bindchange
.
This page has Tencent's english documentation of how to handle events in the WXML. I hope this helps someone!