【问题标题】:Function implementation is missing or not immediately following the declaration. in angular problem with ts声明后缺少或未立即执行函数实现。在 ts 的角度问题中
【发布时间】:2020-02-12 16:12:10
【问题描述】:

请帮我解决倒计时

我现在得到的错误如下: 声明后缺少或未立即执行函数实现。

这是来自 app.component.html

<div app-landing>

</div>

这是html代码

<div class="container">

    <!-- LEFT SIDE -->


    <div class="split left" id="upComing">
        <h1 id="uce">THE UPCOMING EVENT</h1>
        <table id="tableTimer">
            <tr id="timerValue">
                <td id="days"></td>
                <td id="hrs"></td>
                <td id="mins"></td>
                <td id="secs"></td>

            </tr>
            <tr id="timerSub">
                <td>Days</td>
                <td>Hours</td>
                <td>Minutes</td>
                <td>Seconds</td>

            </tr>
        </table>
    </div>


    <!-- RIGHT SIDE -->


    <div class="split right" id="nextFive">
        <h1 id="nfe">THE NEXT FIVE EVENTS</h1>
        <table>

        </table>
    </div>
</div>


<div class="footer" id="footer"></div>

这是 ts 文件。我对 Angular 和 TS 完全陌生,帮我解决问题 想在桌子旁边显示倒数计时器。而且做不到。


    enter code hereimport { Component, OnInit } from '@angular/core';

@Component({
  selector: '[app-landing]',
  templateUrl: './landing.component.html',
  styleUrls: ['./landing.component.css']
})
export class LandingComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

  countDown() {
    var today, eventDay, currentTime, eventTime, remainTime, sec, min, hr, days;

    today = new Date();
    eventDay = new Date();

    currentTime = today.getTime();
    eventTime = eventDay.getTime();

    remainTime = eventTime - currentTime;
    sec = Math.floor(remainTime / 1000);
    min = Math.floor(sec / 60);
    hr = Math.floor(min / 60);
    days = Math.floor(hr / 24);

    hr %= 24;
    min %= 60;
    sec %= 60;

    hr = (hr < 10) ? '0' + hr : hr;
    min = (min < 10) ? '0' + min : min;
    sec = (sec < 10) ? '0' + sec : sec;

    document.getElementById('days').innerText = days;
    document.getElementById('days').textContent = days + '' + ':';
    document.getElementById('hrs').textContent = hr + '' + ':';
    document.getElementById('mins').textContent = min + '' + ':';
    document.getElementById('secs').textContent = sec;

    setInterval(this.countDown, 1000);


}

countDown();

【问题讨论】:

    标签: html css angular typescript web


    【解决方案1】:
    1. 您正在组件类的主体中调用countdown()。这不是有效的 JavaScript。您应该从 ngOnInit() 调用 this.countdown();。
    ngOnInit(): void {
      this.countdown();
    }
    

    ngOnInit 是 Angular 框架的一部分,由 Angular 在加载的组件(如果存在)时运行。

    1. 您正在手动操作 DOM。这不是“角度方式”。相反,您更新模型(组件上的属性),并绑定到 HTML 中的模型以更新 DOM。

    html

    
    <!-- instead of -->
    <td id="days"></td>
    
    <!-- bind read-only values using interpolation -->
    <td>{{days}}</td>
    

    ts

    days: number;
    
    countdown(): void {
        const today = new Date();
        const eventDay = new Date();
    
        const currentTime = today.getTime();
        const eventTime = eventDay.getTime();
    
        const remainTime = eventTime - currentTime;
    
        let sec = Math.floor(remainTime / 1000);
        let min = Math.floor(sec / 60);
        let hr = Math.floor(min / 60);
        let days = Math.floor(hr / 24);
    
        hr %= 24;
        min %= 60;
        sec %= 60;
    
        hr = (hr < 10) ? '0' + hr : hr;
        min = (min < 10) ? '0' + min : min;
        sec = (sec < 10) ? '0' + sec : sec;
    
        this.days = days;
    
        // TODO: add and set the other properties
    
        setInterval(() => this.countDown(), 1000);
    }
    

    编辑

    您应该遵循官方 Angular 教程:https://angular.io/tutorial。这是一个很好的起点。

    【讨论】:

    • 非常感谢,我按照您的说法修复了它。倒数计时器不会自行改变,您能帮我解决一下吗
    • 我建议您将到目前为止所做的事情添加到堆栈闪电战中,然后调整您所做的事情变得更加容易:stackblitz.com/fork/angular
    • 你能告诉我如何打开你提供的链接中的终端
    • 如果要添加依赖项,请转到文件下方的左下角。您通常无法在基于云的编辑器中访问终端
    【解决方案2】:

    像这样简单地改变你的代码

    ts 文件

      ngOnInit(){
       this.countDown();
      }
      timer={days:'',hr:'',min:'',sec:''}
    
    
       countDown() {
        var today, eventDay, currentTime, eventTime, remainTime, sec, min, hr, days;
        window.setInterval(()=>{
        today = new Date();
        eventDay = new Date('2020-02-14');
        currentTime = today.getTime();
        eventTime = eventDay.getTime();
    
        remainTime = eventTime - currentTime;
        sec = Math.floor(remainTime / 1000);
        min = Math.floor(sec / 60);
        hr = Math.floor(min / 60);
        days = Math.floor(hr / 24);
        hr %= 24;
        min %= 60;
        sec %= 60;
       this.timer={sec,days,min,hr}
    
     }
    

    像这样在你的 html 中

    <div class="container">
    
    <!-- LEFT SIDE -->
    
    
    <div class="split left" id="upComing">
        <h1 id="uce">THE UPCOMING EVENT</h1>
        <table id="tableTimer">
            <tr id="timerValue">
                <td id="days">{{timer.days}}</td>
                <td id="hrs">{{timer.hr}}</td>
                <td id="mins">{{timer.min}}</td>
                <td id="secs">{{timer.sec}}</td>
    
            </tr>
            <tr id="timerSub">
                <td>Days</td>
                <td>Hours</td>
                <td>Minutes</td>
                <td>Seconds</td>
    
            </tr>
        </table>
    </div>
    
    
    <!-- RIGHT SIDE -->
    
    
    <div class="split right" id="nextFive">
        <h1 id="nfe">THE NEXT FIVE EVENTS</h1>
        <table>
    
        </table>
    </div>
    

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 2021-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-01
      • 2016-03-17
      • 1970-01-01
      相关资源
      最近更新 更多