Aurelia navigation bar virtual machine does not work

I set up nav bar according to the example in the Aurelia tutorial HTML and nav bar js. Later, I wanted to work at nav bar JS VM adds some functions, but it is found that its properties or methods have not been called

I'm trying to use Aurelia auth filtering in my top navigation, but even if the auth function is omitted, I can't call top nav bar JS

The code is as follows:

Top nav bar js

import {bindable} from 'aurelia-framework';
import {inject} from 'aurelia-framework';
import {AuthService} from 'aurelia-auth';
//import {AuthFilterValueConverter} from './authFilter';
//import {Router} from 'aurelia-router';
@inject(AuthService )
export class TopNavBar {
  _isAuthenticated=false;
  @bindable router = null;

  constructor(auth){
    console.log('called nav bar constructor'); //NEVER CALLED
    this.auth = auth;

  }
  //@computedFrom(this.auth)
  get isAuthenticated(){
    return this.auth.isAuthenticated(); //NEVER CALLED
  }
}

Top NAV - one bar html

<template>
  <nav class="navbar navbar-default navbar-fixed-top" role="navigation">
  <!-- <require from="paulvanbladel/aurelia-auth"></require> -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
        <span class="sr-only">Toggle Navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">
        <i class="fa fa-home"></i>
        <span>${router.title}</span>
      </a>
    </div>

    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li repeat.for="row of router.navigation | authFilter: isAuthenticated" class="${row.isActive ? 'active' : ''}">
          <a data-toggle="collapse" data-target="#bs-example-navbar-collapse-1.in" href.bind="row.href">${row.title}</a>
        </li>
      </ul>

      <ul if.bind="!isAuthenticated" class="nav navbar-nav navbar-right">
      <li><a href="/#/login">Login</a></li>
      </ul>
      <ul if.bind="isAuthenticated" class="nav navbar-nav navbar-right">
        <li><a href="/#/profile">Profile</a></li>
        <li><a href="/#/logout">logout</a></li>
      </ul>

      <ul class="nav navbar-nav navbar-right">
        <li class="loader" if.bind="router.isNavigating">
          <i class="fa fa-spinner fa-spin fa-2x"></i>
        </li>
      </ul>
    </div>
  </nav>
</template>

app. html

<template>
  <require from="./top-nav-bar.html"></require>
  <require from="bootstrap/css/bootstrap.css"></require>

  <top-nav-bar router.bind="router"></top-nav-bar>

  <div class="page-host">
    <router-view></router-view>
  </div>
</template>

Solution

If you have an Aurelia VM with JS and HTML, you need to import it into the view (excluding *. HTML):

<require from="./top-nav-bar"></require>

Sometimes you don't have a view VM. In this case, you only import HTML, just like you:

<require from="./top-nav-bar.html"></require>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>