Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 余烬JS-车把模板-@索引未定义在每个_Javascript_Templates_Ember.js_Ember Cli_Handlebars.js - Fatal编程技术网

Javascript 余烬JS-车把模板-@索引未定义在每个

Javascript 余烬JS-车把模板-@索引未定义在每个,javascript,templates,ember.js,ember-cli,handlebars.js,Javascript,Templates,Ember.js,Ember Cli,Handlebars.js,我找了一个重复的问题,但在上面找不到。这就是它的由来 我的模板中有一个#每个块,它成功地打印了数组中的值,但是当我尝试在该块中使用@index获取当前索引时,像这样-{@index},它是未定义的 从中,我找到了获取当前索引的方法 路由器(Router.js): Router.map(function() { this.resource("books", function(){ this.route("about", { path : "/:bookId"}, funct

我找了一个重复的问题,但在上面找不到。这就是它的由来

我的模板中有一个
#每个
块,它成功地打印了数组中的值,但是当我尝试在该块中使用
@index
获取当前索引时,像这样-
{@index}
,它是未定义的

从中,我找到了获取当前索引的方法

路由器(Router.js):

Router.map(function() {
    this.resource("books", function(){
        this.route("about", { path : "/:bookId"}, function(){

        });
    });
});
路线(routes/books.js):

var BooksRoute = Em.Route.extend({
    model: function() {
        return [{
            name: "Harry Potter & The Deathly Hallows"
        }, {
            name: "What If?"
        }, {
            name: "Diary of a Wimpy Kid"
        }, {
            name: "The Martian"
        }];
    }
});

export default BooksRoute;
<div class="page-header">
    <h1>All Books!</h1>
    <ul>
        {{#each book in model}}
            {{@index}}
            <li>{{#link-to "books.about"}} {{book.name}} {{/link-to}}</li>
        {{/each}}
    </ul>
</div>
模板(模板/书籍/索引.hbs):

var BooksRoute = Em.Route.extend({
    model: function() {
        return [{
            name: "Harry Potter & The Deathly Hallows"
        }, {
            name: "What If?"
        }, {
            name: "Diary of a Wimpy Kid"
        }, {
            name: "The Martian"
        }];
    }
});

export default BooksRoute;
<div class="page-header">
    <h1>All Books!</h1>
    <ul>
        {{#each book in model}}
            {{@index}}
            <li>{{#link-to "books.about"}} {{book.name}} {{/link-to}}</li>
        {{/each}}
    </ul>
</div>

所有的书!
    {{#模型中的每本书} {{@index}}
  • {{{#链接到“books.about”}{{{book.name}{{{/link to}}
  • {{/每个}}
我希望能够生成指向每本书的链接,当前索引用作
:bookId
,例如
/books/1

我的代码有什么错误吗?我怎样才能让它工作

更新:我也尝试了
{{{#每种模型作为|图书索引|}}
,但没有成功

它与
{{#每个模型作为|图书索引|}}
一起工作。问题是我用新方法做了
{{@index}}
而不是
{{index}}

你可以像这样使用它

{{#each model as |book index|}}
  {{index}}
        <li>{{#link-to "books.about" index}} {{book.name}} {{/link-to}}</li>
{{/each}}
{{{#每个模型作为|图书索引}
{{index}}
  • {{{#链接到“books.about”索引}{{{book.name}{{{/链接到}}
  • {{/每个}}

    试着使用这个{{每个模型作为{图书索引}{图书索引}{li>{{链接到“books.about”}{{{book.name}{{/link到}{/li>{/each}没有运气。仍然
    未定义
    是,它在演示中有效,但在我的项目中无效(对不起,它确实有效。我的错。我还在试着打印
    {{@index}}
    。非常感谢。你能把它作为一个答案添加进来,这样我就可以接受了吗?这似乎是一个比
    @index
    更好的方法,索引现在可以更自然地传递给其他东西了。