有没有办法使用"模板参考变量"与Angular 4?

时间:2017-10-05 10:58:06

标签: angular pug

我正在尝试计算如何在.pug模板中使用模板引用变量。

例如:div(#var)会抛出错误:

compiler.es5.js:1694 Uncaught Error: Template parse errors: There is no directive with "exportAs" set to "#var" (" ... 

原因是哈巴狗将呈现为:

<div #var="#var"> ...

Angular将失败。

2 个答案:

答案 0 :(得分:6)

来自doc

  

模板参考变量(#var)

     

模板引用变量通常是对DOM元素的引用   在模板中。它也可以是对Angular组件的引用   或指令或网络组件。

所以,你需要<div #var >,#var在这个var元素上声明一个<div>变量。

  

在大多数情况下,Angular将引用变量的值设置为   声明它的元素.... 但指令可以改变它   行为并将值设置为其他内容,例如它自己。该   NgForm指令执行此操作

如果您在模板引用变量中指定了某些内容,则它应该是指令组件,例如:#var="ngForm" ngForm是一个内置的指令。

这就是您收到错误的原因:There is no directive with "exportAs" set to "#var"

因为 #var (您已分配:<div #var="#var">)不是组件也不是指令,

现在对于jade(pug),如果你想要一个null属性,你应该设置编译器to compile to HTML doctype,因为pug的默认行为是设置属性上的属性值:

默认行为:

div(#var)已编译为:<div #var="#var"></div>

div(hidden)已编译为<div hidden="hidden"></div>

带有doctype html的

div(#var)已编译为:<div #var></div>

div(hidden)已编译为<div hidden></div>

或者你可以为你想要的每个文件放入文件的开头:doctype html

答案 1 :(得分:1)

我有同样的情况,正在寻找解决方法。

我找到了这个解决方案:

示例:import httplib2 from apiclient import errors from apiclient.discovery import build from oauth2client.client import OAuth2WebServerFlow # Copy your credentials from the console CLIENT_ID = 'XXXXXXXXXXXXXXXXXXX' CLIENT_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' # Check https://developers.google.com/webmaster-tools/search-console-api-original/v3/ for all available scopes OAUTH_SCOPE = 'https://www.googleapis.com/auth/webmasters.readonly' # Redirect URI for installed apps REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob' # Run through the OAuth flow and retrieve credentials flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI) authorize_url = flow.step1_get_authorize_url() print ('Go to the following link in your browser: ' + authorize_url) code = input('Enter verification code: ').strip() credentials = flow.step2_exchange(code) # Create an httplib2.Http object and authorize it with our credentials http = httplib2.Http() http = credentials.authorize(http) webmasters_service = build('webmasters', 'v3', http=http) # Retrieve list of properties in account site_list = webmasters_service.sites().list().execute() # Filter for verified websites verified_sites_urls = [s['siteUrl'] for s in site_list['siteEntry'] if s['permissionLevel'] != 'siteUnverifiedUser' and s['siteUrl'][:4] == 'http'] # Printing the URLs of all websites you are verified for. for site_url in verified_sites_urls: print (site_url) # Retrieve list of sitemaps submitted sitemaps = webmasters_service.sitemaps().list(siteUrl=site_url).execute() if 'sitemap' in sitemaps: sitemap_urls = [s['path'] for s in sitemaps['sitemap']] print (" " + "\n ".join(sitemap_urls)) testear = webmasters_service.searchanalytics() print (testear) request = { "startDate": "2018-10-10", "endDate": "2018-10-12", "searchType": "web", "dimensions": [ "page", "query" ] } response = oauth2.Request(webmasters_service, "http://www.valenciaeventosnauticos.com", request) print(response)

资源: https://medium.com/paramsingh-66174/pugjs-primer-for-your-next-angular-4-project-409ee6696fad