<script language="javascript" for="Player" event="NewRealAlarmNotify(json)" type="text/javascript">
NewRealAlarmNotify(json);
</script>
这种写法在HTML上写没问题,在VUE上就不适用了。换个思路,在mounted内动态生成script节点:
var oScript = document.createElement('script');
window.phoneListener = this;
oScript.type = 'text/javascript';
oScript.setAttribute('for','Player');
oScript.setAttribute('language','javascript');
oScript.event = 'NewRealAlarmNotify(json)';
oScript.innerHTML='phoneListener.NewRealAlarmNotify(json)';
document.body.appendChild(oScript);
值得注意的是:此方法会将<script>function</script>渲染到页面上,但是想执行Vue的methods的方法的话,还需要在mounted里加上一句代码: window.phoneListener = this;
这段代码的作用是将Vue的this所指向赋值给全局的变量phoneListener,这个变量,可随意。