对于想了解vue报错--templatesyntaxerrorComponenttemplateshouldcontainexactlyonerootelement的读者,本文将是一篇不可错过的文章,
对于想了解vue 报错 --template syntax error Component template should contain exactly one root element的读者,本文将是一篇不可错过的文章,并且为您提供关于# [译]angular中的ng-template, ng-content, ng-container、ActionView::SyntaxErrorInTemplate in AccountsController#profile、angular2 ng-container如何使用动态ngTemplateOutletContext、angularJs component dynamic template的有价值信息。
本文目录一览:- vue 报错 --template syntax error Component template should contain exactly one root element
- # [译]angular中的ng-template, ng-content, ng-container
- ActionView::SyntaxErrorInTemplate in AccountsController#profile
- angular2 ng-container如何使用动态ngTemplateOutletContext
- angularJs component dynamic template
vue 报错 --template syntax error Component template should contain exactly one root element
在读 vue 文档的时候,感觉在介绍.vue 模板的时候写的很随意,什么不清楚到底 vue-loader 编译后的组件和不用.vue 组件有什么异同。果然在使用的过程中遇到里很多低级的问题。
下面是我报错的模板
<template>
<div>
index{{msg}}
</div>
<router-link to="/">Go to Bar</router-link>
</template>
<style>
</style>
<script>
export default {
data() {
return {
msg: ''请叫我守夜''
}
}
}
</script>
报错信息
template syntax error Component template should contain exactly one root element:
错误信息很明确,模板只应该有一个根元素
修改
<template>
<div>
index{{msg}}
<router-link to="/">Go to Bar</router-link>
</div>
</template>
<style>
</style>
<script>
export default {
data() {
return {
msg: ''请叫我守夜''
}
}
}
</script>
总结:模板所有的内容都应该在一个根元素内,通常会用给一个 div 来包裹其他元素
# [译]angular中的ng-template, ng-content, ng-container
本文会包含作者一些理解,为了不和原作者意思混淆,会将个人的理解放在【】里。
原文地址
那是我忙于为我的办公项目创作新功能的日子之一。突然间,某些事情引起了我的注意。
上图是angular最终渲染出来的DOM
当我审查DOM时候,我看到这个 ngcontent
被Angular应用到了元素上。如果他们包含元素在最终的DOM里,那么<ng-container>
是用来干什么的? 那是我很困惑在 <ng-container>
和 <ng-content>
中。
在探寻问题答案的时候我发现了<ng-template>
和 *ngTemplateOutlet
的概念。现在我们有四个听起来几乎相同的概念。我开始了探寻它们的旅程。
你曾经也遇到过这种情况吗?如果是,那么你来对地方了。闲话少说,让我们一个一个来讲解他们。
1. < ng-template >
顾名思义,<ng-template>
是一个模板元素,用于Angular与结构指令结合使用(*ngIf
, *ngFor
, [ngSwitch]
和自定义指令)
<font color=''red''>这些模板元素仅工作在结构指令存在的时候</font>。Angular包装了我们应用指令的元素。 考虑下边的一个*ngIf
的例子。
【我们可以看到,在一个普通元素上添加结构指令,angular在解析时候,其实会帮我买创建一个ng-template元素,并将相关的结构指令放到ng-template上进行处理】
上面展示的是Angular对与*ngIf
的解释。 最终的DOM与我们在本文开头看到的类似。
使用方法
我们已经看到了Angular使用<ng-template>
的方式。但是我们如何使用它呢?由于它只和结构指令一起工作,所以我们可以这样使用:【这种是❌方式,后边会讲到】
此处的 home
设置为 true
, 上述代码在DOM中的输出为:
什么都没有被渲染出来!这是为什么呢? 其实,这正是预期的结果。正如我们已经讨论过的,Angular会用注释替代ng-template
【译文使用的Angular版本为6.1.10。 在Angular12版本,已经用特殊的className代替注释了】,我们的代码最终会被解释成如下代码。
我们的<ng-template>
在被Angular包装之后,变成了两个。但是无论怎样,Angular都不对<ng-template>
的内容进行选中。
以下是两种<font color=green>正确</font>的使用方式:
Method 1
在第一种方法中,你提供给Angular一种不需要进一步处理的方式。这样Angular将仅仅转换 <ng-template>
到注释里,并不会改变其内容。(它们不会再像之前的例子一样被放在任何<ng-template>
中)。因此,它将正确的渲染内容。
要想了解更多如何使用此结构和更多的结构指令,请参考这篇文章。
Method2
这是一种很少使用的方式(使用两个<ng-template>
)。这里我们在then中给出了一个模板引用,告诉他当条件为真时候应该渲染什么。
像这样使用多<ng-template>
(你可以使用<ng-container>
代替)是不被建议的因为这不是Angular的本意。<ng-template>
应当被使用在多处重复使用的场景下。我们将在文章后面的部分更详细的讨论。
< ng-container >
你是否曾经写过或看到过类似这样的代码
我们大部分人写这样代码的原因在于Angular无法再单个元素上使用多个结构指令。现在这个代码正常工作,但是如果item.id 为false,它就会在dom中引入很多空的<div>
在简单的例子中可能不需要关心它,但如果在大型复杂的应用中时候(会展示成千上万的数据),它可能变得麻烦,因为有可能会有监听器在这些DOM上。
更糟糕的是你可能必须去嵌套你的CSS。【大概是想表达这些写会在附加样式时候需要额外的选择器】
不要担心!我们有<ng-container>
可以解决。
<ng-container>
是一个组元素,它不会干扰样式或布局,因为Angular不会将它们渲染到DOM中。
以上代码会渲染出这样的DOM结构
看,我们摆脱了那些空的div。我们应该使用<ng-container>
当我们仅仅想使用多个结构指令而不想引入多的DOM时候。
3、< ng-content >
它用于创建可配置的组件。这意味着组件可以根据用户的意愿来配置。这就是众所周知的内容投影~
考虑一个简单的使用了<ng-content>
的组件
在<project-content>
组件的开始标记和结束标记之间是将要被内容投影的内容。【也就是vue和react的slot】
这些内容将被渲染在组件的<ng-content>
中,这将允许自定义<project-content>
组件的页脚部分。
多重投影
如果你可以决定哪些内容可以被渲染到哪些地方会怎样?您还可以使用<ng-content>
的select属性来控制内容的投影方式,而不是在单个的<ng-content>
中投影每个内容。它需要一个元素选择器来决定特定的<ng-content>
中投影哪些内容.
方法如下:
我们修改<project-content>
来演示如何使用多内容投影。Select属性选择器将在特定的<ng-content>
中呈现的内容。这里我们首先选择呈现h1元素,如果投射的内容中没有h1元素,它将不会渲染任何内容。同样,第二个选择查找div。其余的内容在最后一个<ng-content>
中呈现。
以上的方式应该是老版本的Angular了,现在的Angular文档中没有提到,现在的使用方式请查看最新文档
4、*ngTemplateOutlet
*ngTemplateOutlet
一般被用在两个场景:
- 不论循环或条件如何,在视图中插入一个公共模板
- 创建一个高度配置的组件
模版重用
考虑一个视图,其必须在多个位置插入模板。例如,要放置在网站中的公司logo。我们可以通过为logo编写一次模板并在视图的任何地方重用来实现它。
下面是代码片段:
正如你看到的,我们只写了一次模板代码,并使用了它三次!
<*ngTemplateOutlet>
也接受一个上下文对象,可以传递改对象来自定义通用模板输出。有关上下文的更多信息,请参阅官方文档
自定义组件
<*ngTemplateOutlet>
的第二个使用方式是高度订制组件。考虑我们之前的<project-content>
的示例。并进行一些修改。
上面是<project-content>
组件的修改版本,它接受三个属性 —— <headerTemplate>
<bodyTemplate>
<footerTemplate>
。下面是其代码片段:
我们在这里视图实现的是显示从<project-content>
的父组件接受到的页眉,正文和页脚。如果其中任何一个未提供,我们的组件在其位置展示默认模版。因此,创建了一个高度订制的组件。
现在使用我们刚创建的组件:
这是我们如何传递一个 template refs 给我们的组件的方式。如果其中任何一个没有传递,组件将渲染默认的模板!
ng-content vs *ngTemplateOutlet
它们都帮我们实现了高度自定义的组件,但是我们该如何选择它们呢?
可以清除的看到,*ngTemplateOutlet
给了我们更多的能力,例如提供默认的模板...
但 <ng-content>
智能按照原样呈现内容,借助select属性,您可以拆分内容并在视图的不同位置呈现它们。您不能有条件的呈现<ng-content>
的内容。您必须限制从父组件哪里接受到的内容,而无法根据内容做出决定。
无论如何,在这两个间进行选择完全取决于你的用例。至少现在我们有了一个新的武器 *ngTemplateOutlet
,它提供了更多的控制权对比<ng-content>
ActionView::SyntaxErrorInTemplate in AccountsController#profile
如何解决ActionView::SyntaxErrorInTemplate in AccountsController#profile?
当结束时意外结束,但我看不出问题出在哪里。 我的终端不断给我错误:ActionView::SyntaxErrorInTemplate(渲染模板时遇到语法错误:
我的 profile.html.erb
<% if @users.image.present? %>
<%= image_tag @users.image %>
<% end %>
<strong><h1><%= @users.full_name %></h1></strong>
<% if user_signed_in? %>
<% if @user == current_user %>
<%= link_to"Edit Profile",edit_user_registration_path(@user) %>
<% if user_signed_in? && !@user? %>
<% if current_user.following?(@user) %>
<%= link_to"Unfollow",follows_path(user_id: @user.id),method: :delete %>
<% else %>
<%= link_to"Follow",follows_path(user_id: @user.id) %>
<% end %>
<% end %>
<% end %>
<% end %>
<div> <%= @users.posts.count %> Posts </div>
<p><%= @users.full_name %></p>
<p><%= @users.description %></p>
<p><%= link_to @users.website if @users.website.present? %></p>
<%= @posts.each do |post|%>
<%= image_tag post.image %>
<% end %>
解决方法
问题似乎在这里
<p><%= link_to @users.website if @users.website.present? %></p>
改成
<p><%= link_to ''User Website'',@users.website if @users.website.present? %></p>
,
<% if user_signed_in? && @user == current_user %>
<%= link_to"Edit Profile",edit_user_registration_path(@user) %>
<% if current_user.following?(@user) %>
<%= link_to"Unfollow",follows_path(user_id: @user.id),method: :delete %>
<% else %>
<%= link_to"Follow",follows_path(user_id: @user.id) %>
<% end %>
<% end %>
现在好了:D
,我看到了两个错误,请纠正。首先,没有什么 @user?
是最大的错误,另一个可能不是问题,但没有空间 <%= link_to"Edit Profile"
请尝试以下代码,我已更改内容并正确重新格式化。
<% if @users.image.present? %>
<%= image_tag @users.image %>
<% end %>
<strong><h1><%= @users.full_name %></h1></strong>
<% if user_signed_in? %>
<% if @user == current_user %>
<%= link_to "Edit Profile",edit_user_registration_path(@user) %>
<% if user_signed_in? && !@user %>
<% if current_user.following?(@user) %>
<%= link_to"Unfollow",method: :delete %>
<% else %>
<%= link_to"Follow",follows_path(user_id: @user.id) %>
<% end %>
<% end %>
<% end %>
<% end %>
<div> <%= @users.posts.count %> Posts </div>
<p><%= @users.full_name %></p>
<p><%= @users.description %></p>
<p><%= link_to @users.website if @users.website.present? %></p>
<%= @posts.each do |post|%>
<%= image_tag post.image %>
<% end %>
angular2 ng-container如何使用动态ngTemplateOutletContext
<div *ngFor="let child of children; let i=index"> <ng-container *ngTemplateOutlet="inputTpl; { $implicit: child,index: i }"></ng-container> </div>
这会导致错误
Unexpected token {,expected identifier,keyword,or string at column 11 in [inputTpl; { $implicit: child,index: i }]
当我在文档中使用’context:’时,这会导致
Can’t bind to ‘ngTemplateOutletContext’ since it isn’t a kNown property of ‘ng-container’
如果我使用在我的ts文件中声明的对象并设置它而不是我的对象,一切都运行正常.此外,这也很好:
<div *ngFor="let child of children; let i=index"> <template [ngTemplateOutlet]="inputTpl" [ngOutletContext]="{ $implicit: child,index: i }" /> </div>
有谁知道,我如何使用带有* ngTemplateOutlet的ng-container和html中生成的ngTemplateOutletContext?
解决方法
<ng-container [ngTemplateOutlet]="inputTpl" [ngTemplateOutletContext]="{ $implicit: child,index: i }" > </ng-container>
angularJs component dynamic template
<pre><code>最近我司开始进行这样<a href="https://www.jb51.cc/tag/yige/" target="_blank">一个</a>工作,将内部通用组件全部写到github上去并<a href="https://www.jb51.cc/tag/zhuce/" target="_blank">注册</a>到bower中,然后引入项目减少<a href="https://www.jb51.cc/tag/daima/" target="_blank">代码</a>量
在开发过程中我碰到了这样一个问题,在写组件时因为是component而不是directive所以不能构建动态模板并不是那么方便,如果采用相对路径的方式那么这样就会产生一个问题,引入到实际项目中时,由于模版路径不一致,这样将会无法找到对应的模板。
于是在不断的谷歌过程中,我们找到了以下几种可以component引入模板的解决方案
1、在template中写(这样代码风格相当拙计)
2、在templateUrl中引入模板的相对路径(这样无法构成通用组件)
3、利用$templateCache去动态引入模板(我们是下载了一个gulp-template-cache的bower,帮助我们生成一个存储模板的xx.js文件)
4、在页面中嵌入script【https://plnkr.co/edit/EOmonofrOHijfJv8YJyO?p=preview】
今天的关于vue 报错 --template syntax error Component template should contain exactly one root element的分享已经结束,谢谢您的关注,如果想了解更多关于# [译]angular中的ng-template, ng-content, ng-container、ActionView::SyntaxErrorInTemplate in AccountsController#profile、angular2 ng-container如何使用动态ngTemplateOutletContext、angularJs component dynamic template的相关知识,请在本站进行查询。
本文标签: