🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
#### G.url方法 模版引擎生成url方法 模版标签调用方法: ~~~ {% set id = '1' %} {{ G.url('app/index/index',{'id':id})}} ~~~ 普通模式下输出: ~~~ http://test.calfbb.com/index.php?m=app&c=index&a=index&id=1 ~~~ 伪静态模式下输出 ~~~ http://test.calfbb.com/index.php/app/index/index/id/1.html ~~~ js调用方法 ~~~ va testid=1; var url="{{ G.url('app/index/index',{'id':'testid'})}}"; console.log(url); ~~~ 普通模式输出 (伪静态模式输出跟上面一致) ~~~ http://test.calfbb.com/index.php?m=app&amp;c=index&amp;a=index&amp;id=testid ~~~ 注意:testid没有被替换,原因是G.url是php函数不是js函数,testid是js变量,不是php变量,解决方法如下: ~~~ var testid=1; var url="{{ G.url('app/index/index',{'id':'testid'})}}"; url = url.replace("testid", testid);//使用js自带函数进行变量替换 console.log(url); ~~~ 输出: ~~~ http://test.calfbb.com//index.php?m=app&amp;c=index&amp;a=index&amp;id=1 ~~~ 最后再过滤掉转义字符(如果有&amp字符出现的情况下) ~~~ var testid=1; var url="{{ G.url('app/index/index',{'id':'testid'})}}"; url = url.replace("testid", testid);//使用js自带函数进行变量替换 console.log(escapeUrl(url)); /** * 字符转义 * @param str */ function escapeUrl(str) { var arrEntities={'lt':'<','gt':'>','nbsp':' ','amp':'&','quot':'"'}; return str.replace(/&(lt|gt|nbsp|amp|quot);/ig,function(all,t){return arrEntities[t];}); } ~~~ 输出: ~~~ http://test.calfbb.com/index.php?m=app&c=index&a=index&id=1 ~~~