In the template string, click event parameter passing problem

When I was working on a map project recently, I had to use template strings to write native html, and there was a click event-onclick. I found that there were many problems when passing parameters, so I will record it here.

 When passing a single data, there is no problem. They are all primitive types of data, which can be parsed out, as shown in the figure above; but if the passed data is a reference type, such as an object, an error will be reported (item is an object), as shown in the figure below:

 

 

I don’t know what’s wrong with the conversion. I checked a lot of methods on the Internet. What kind of escaping, + and the like are not very effective, and various errors are reported. Later, I found a method using JSON.stringify(), But it's not used in the template string, and it doesn't work directly. After continuous trial and error, I finally came up with a method and finally passed it on! As shown below:

 Note: here  onclick =  view(${JSON.stringify(item)})  ' , the package uses single quotes  ' ' instead of double quotes! (I don't know why single quotes can be double quotes.. ....)

Hope someone who knows can give some pointers! The above is the solution process and results.

 

Guess you like

Origin blog.csdn.net/weixin_64482155/article/details/128867931