首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >React : TypeError:无法读取未定义的属性“视频”

React : TypeError:无法读取未定义的属性“视频”
EN

Stack Overflow用户
提问于 2021-07-15 02:05:59
回答 1查看 362关注 0票数 1

我在左边有一个章节列表,右边有下一个/前面的按钮。(如以下屏幕所示)

当我在章节之间导航时,它工作得很好。当我只点击下一个/前一个按钮时,它也能正常工作。但是,当我选择一章,然后单击next或or,就会得到错误:TypeError:无法读取未定义的属性“视频”。

我的组成部分:

代码语言:javascript
运行
复制
import React, { Component, useEffect, useState } from 'react';
import './App.css';

var source = '/Videos/Part1.mp4';

var videos = [
  {
    Chapitre: 'Intro',
    video: '/Videos/part0 - training 1.mp4'
  },
  {
    Chapitre: 'The need of 5G',
    video: '/Videos/Part1.mp4'
  },
  {
    Chapitre: '5G potential use cases',
    video: '/Videos/Part 2.mp4'
  },
  {
    Chapitre: '5G technical requirements',
    video: '/Videos/Part 3.mp4'
  },
  {
    Chapitre: 'Timeline for 5G standards and roll out',
    video: '/Videos/Part 4.mp4'
  },
  {
    Chapitre: 'Quiz',
    video: '/Videos/Part 5.mp4'
  }
];

class App extends Component {
  constructor(props) {
    super(props)
    this.state = {
      indice: 0
    }
    //this.SelectChapiter = this.SelectChapiter.bind(this);
    this.Next = this.Next.bind(this);
    //this.Previous = this.Previous.bind(this);
  }
  

  SelectChapiter(index) {
    this.setState({ indice: index });
    document.getElementById('change').src = videos[index].video;
};

Next () {
  alert(videos[this.state.indice].video)
  if(this.state.indice != 5){
    //this.setState({ indice: this.state.indice + 1 });
    this.SelectChapiter(this.state.indice + 1);
    
  }
};

Previous () {
  alert(this.state.indice)

  if(this.state.indice != 0){
    //this.setState({ indice: this.state.indice + 1 });
    this.SelectChapiter(this.state.indice - 1);
}
}
  render() { 
    return (
      <div>
        <div className="LeftNav">
          <div className="FormationName">5G Associate Training_01M01</div>
          <div id="Obj">Objective of training</div>
          <ul className= "chapitres">
            <button onClick={() => this.SelectChapiter('0')}>Intro</button><br/>
            <button onClick={() => this.SelectChapiter('1')}>The need of 5G</button>
            <button onClick={() => this.SelectChapiter('2')}>5G potential use cases</button>
            <button onClick={() => this.SelectChapiter('3')}>5G technical requirements</button>
            <button onClick={() => this.SelectChapiter('4')}>Timeline for 5G standards</button><br/>
            <button onClick={() => this.SelectChapiter('5')}>Quiz</button>
          </ul>
        </div>
  
        <div className="control">
          <input onClick={this.Previous} className="icon" type="image" src="/Assets/previous.png" />
          <input onClick={this.Next} className="icon" type="image" src="/Assets/next.png" />
        </div>
        <video className="VidéoSection" id = "change" controls>
            <source src='/Videos/part0 - training 1.mp4' type="video/mp4"/>
          Your browser does not support the video tag.
        </video>
      </div>
  
      
    );
  }
}

export default App;

应用程序的屏幕:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-15 12:34:11

我终于解决了这个问题。正如菲尔所说,我应该把数字而不是字符串传递到SelectChapiter()中,我还应该绑定Previous

新的守则:

代码语言:javascript
运行
复制
import React, { Component } from "react";
import "./App.css";

var videos = [
  {
    Chapitre: "Intro",
    video: "/Videos/part0 - training 1.mp4",
  },
  {
    Chapitre: "The need of 5G",
    video: "/Videos/Part1.mp4",
  },
  {
    Chapitre: "5G potential use cases",
    video: "/Videos/Part 2.mp4",
  },
  {
    Chapitre: "5G technical requirements",
    video: "/Videos/Part 3.mp4",
  },
  {
    Chapitre: "Timeline for 5G standards and roll out",
    video: "/Videos/Part 4.mp4",
  },
  {
    Chapitre: "Quiz",
    video: "/Videos/Part 5.mp4",
  },
];

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      indice: 0,
    };
    this.Next = this.Next.bind(this);
    this.Previous = this.Previous.bind(this);
  }

  SelectChapiter(index) {
    this.setState({ indice: index });
    document.getElementById("change").src = videos[index].video;
  }

  Next() {
    if (this.state.indice != 5) {
      this.SelectChapiter(this.state.indice + 1);
    }
  }

  Previous() {
    alert(this.state.indice);

    if (this.state.indice != 0) {
      this.SelectChapiter(this.state.indice - 1);
    }
  }
  render() {
    return (
      <div>
        <div className="LeftNav">
          <div className="FormationName">5G Associate Training_01M01</div>
          <div id="Obj">Objective of training</div>
          <ul className="chapitres">
            <button onClick={() => this.SelectChapiter(0)}>Intro</button>
            <br />
            <button onClick={() => this.SelectChapiter(1)}>
              The need of 5G
            </button>
            <button onClick={() => this.SelectChapiter(2)}>
              5G potential use cases
            </button>
            <button onClick={() => this.SelectChapiter(3)}>
              5G technical requirements
            </button>
            <button onClick={() => this.SelectChapiter(4)}>
              Timeline for 5G standards
            </button>
            <br />
            <button onClick={() => this.SelectChapiter(5)}>Quiz</button>
          </ul>
        </div>

        <div className="control">
          <input
            onClick={this.Previous}
            className="icon"
            type="image"
            src="/Assets/previous.png"
          />
          <input
            onClick={this.Next}
            className="icon"
            type="image"
            src="/Assets/next.png"
          />
        </div>
        <video className="VidéoSection" id="change" controls>
          <source src="/Videos/part0 - training 1.mp4" type="video/mp4" />
          Your browser does not support the video tag.
        </video>
      </div>
    );
  }
}

export default App;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68387027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档