ember.js路径和模板不工作

wfauudbj  于 2022-09-28  发布在  其他
关注(0)|答案(1)|浏览(145)

我在application.hbs中包含了一个组件

<Auth></Auth>

接下来,在auh组件中,我有以下代码auth.hbs

<form>
    <div class="text-field">
         <label>UserName:</label>
        <Input type="text" id="username" required />
        <span></span>

    </div>
    <div class="text-field">
        <label>Password:</label>
        <Input type="password" id="password" required />
        <span></span>

    </div>
    <div id="result"></div>
    <input {{action "buttonClick"}} type="submit" value="Login">
</form>

授权.js

import Component from '@glimmer/component';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';

export default class AuthComponent extends Component {
  @service router;
   @service('index') auth;

  @action
  buttonClick() {
    let username = document.getElementById('username').value;
    let password = document.getElementById('password').value;
    console.log('user is ' + username);
    var dummy = this;
    dummy.router.replaceWith('demo');
    this.get('router.router').transitionTo('demo');
}

然后是路由器演示。hbs

{{#each @model as |post|}}
  <div>
    {{post.title}}
  </div>
{{/each}}

演示.js

import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';

export default class DemoRoute extends Route {
  @service router;
  async model() {
    return [
      { title: 'Ember Roadmap' },
      { title: 'Accessibility in Ember' },
      { title: 'EmberConf Recap' },
    ];
  }
}

因此,当我单击登录按钮时,我需要查看从route返回的内容,但没有查看新页面,只有登录页面在那里,但url更改为http://localhost:8035/foodapp/#/demo但页面没有更改
路由器.js

import EmberRouter from '@ember/routing/router';
import config from 'foods/config/environment';

export default class Router extends EmberRouter {
  location = config.locationType;
  rootURL = config.rootURL;
}

Router.map(function () {
  // this.route('request', { path: '/req' });
  this.route('demo', { path: '/demo',resetNamespace: true });

  // this.route('index', { path: '/' }); // this is implicit, just putting it in for clarity
  // this.route('fo', function() {
  //   this.route('index', { path: '/' }); // this is implicit, just putting it in for clarity
  //   this.route('post', { path: '/:id' });
  // });

});

任何人都可以帮助我如何查看与路由相关的模板。如何通过转换到路由从登录页面转到新页面。。
环境.js

'use strict';

module.exports = function (environment) {
  let ENV = {
    modulePrefix: 'foods',
    environment,
    rootURL: '/',
    locationType: 'hash',
    EmberENV: {
      FEATURES: {
        // Here you can enable experimental features on an ember canary build
        // e.g. EMBER_NATIVE_DECORATOR_SUPPORT: true
      },
      EXTEND_PROTOTYPES: {
        // Prevent Ember Data from overriding Date.parse.
        Date: false,
      },
    },

    APP: {
      // Here you can pass flags/options to your application instance
      // when it is created
    },
  };

  if (environment === 'development') {
    // ENV.APP.LOG_RESOLVER = true;
    // ENV.APP.LOG_ACTIVE_GENERATION = true;
    // ENV.APP.LOG_TRANSITIONS = true;
    // ENV.APP.LOG_TRANSITIONS_INTERNAL = true;
    // ENV.APP.LOG_VIEW_LOOKUPS = true;
  }

  if (environment === 'test') {
    // Testem prefers this...
    ENV.locationType = 'none';

    // keep test console output quieter
    ENV.APP.LOG_ACTIVE_GENERATION = false;
    ENV.APP.LOG_VIEW_LOOKUPS = false;

    ENV.APP.rootElement = '#ember-testing';
    ENV.APP.autoboot = false;
  }

  if (environment === 'production') {
    // here you can enable a production-specific feature
  }

  return ENV;
};
vdgimpew

vdgimpew1#

如果<Auth></Auth>是您的整个e1d1e,这就解释了您的问题!
重要的是要理解,application.hbs的内容“始终”可见。这就像顶级路线。因此它需要一个{{outlet}},它将是任何子路由的占位符,如示例中的demo。所以你需要添加这个。
此外,resetNamespace: true在顶级路线中没有任何意义,您应该删除它。
重要的是,您的router.js创建了3条路线:

  • 应用程序
  • 索引
  • 演示

如果这没有帮助,您可以验证浏览器控制台中没有任何错误,并指定列出的所有文件的完整路径,因为可能有一个路径错误。

相关问题