账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    求问大佬如何把里面js翻译成ts
    48
    0
    <template> <div class="date"><div class="weeks"> <ul class="days"> <li @click="weekPre" class="prev-btn" > <i class="arr-left"></i> <span class="hidden-sm-and-down" style="margin-left: 5px;" >上一周</span> </li> <li class="date-item" @click="pick(day, index)" v-for="(day, index) in days" :key="index" :class="{selected: index == tabIndex}" > <span v-if="day.getMonth()+1 != currentMonth" class="other-month item-wrapper" > <p>{{day | getWeekFormat}}</p> <span class="hidden-sm-and-down">{{ day | dateFormat }}</span> </span> <span v-else> <span v-if="day.getFullYear() == new Date().getFullYear() && day.getMonth() == new Date().getMonth() && day.getDate() == new Date().getDate()" class="today-item" >今天</span> <span class="item-wrapper" v-else > <p>{{day | getWeekFormat}}</p> <span class="hidden-sm-and-down">{{ day | dateFormat }}</span> </span> </span> </li> <li @click="weekNext" class="next-btn" > <span class="hidden-sm-and-down" style="margin-right: 5px;" >下一周</span> <i class="arr-right"></i> </li> </ul></div></div></template><script>import _ from 'lodash';import moment from 'moment';export default { props: {dateValue: { type: String, default: moment(new Date()).format('YYYY-MM-DD')},timeValue: { type: String, default: '00:00'}}, data() {return { currentYear: 1970, currentMonth: 1, currentDay: 1, currentWeek: 1, days: [], tabIndex: null, newDate: moment(new Date()).format('YYYY-MM-DD')};}, filters: {dateFormat(date) { return moment(date).format('YYYY-MM-DD');},getWeekFormat(date) { const weeksObj = { 1: '周一', 2: '周二', 3: '周三', 4: '周四', 5: '周五', 6: '周六', 7: '周日' }; let weekNumber = moment(date).isoWeekday(); return weeksObj[weekNumber];}},mounted() {// const index = _.findIndex(this.days, function(o) {// return moment(o).dayOfYear() === moment().dayOfYear();// });// this.tabIndex = index;this.initcurrentIndex();},created() {this.initData(null);},methods: {initcurrentIndex(refresh) { this.newDate = refresh || this.newDate; let newDate = this.newDate; let index = _.findIndex(this.days, function (o) { return moment(o).format('YYYY-MM-DD') === moment(newDate).format('YYYY-MM-DD'); }); this.tabIndex = index;},formatDate(year, month, day) { const y = year; let m = month; if (m < 10) m = `0${m}`; let d = day; if (d < 10) d = `0${d}`; return `${y}-${m}-${d}`;},initData(cur) { let date = ''; if (cur) { date = new Date(cur); } else { date = new Date(); } this.currentDay = date.getDate(); this.currentYear = date.getFullYear(); this.currentMonth = date.getMonth() + 1; this.currentWeek = date.getDay(); if (this.currentWeek === 0) { this.currentWeek = 7; } const str = this.formatDate(this.currentYear, this.currentMonth, this.currentDay); this.days.length = 0; for (let i = this.currentWeek - 1; i >= 0; i -= 1) { const d = new Date(str); d.setDate(d.getDate() - i); this.days.push(d); } for (let i = 1; i <= 7 - this.currentWeek; i += 1) { const d = new Date(str); d.setDate(d.getDate() + i); this.days.push(d); }},weekPre() { const d = this.days[0]; d.setDate(d.getDate() - 7); this.initData(d); this.initcurrentIndex();},weekNext() { const d = this.days[6]; d.setDate(d.getDate() + 7); this.initData(d); this.initcurrentIndex();},pick(date, index) { this.newDate = moment(date).format('YYYY-MM-DD'); this.$emit('dateValue', this.newDate); console.log('this.newDate: ', this.newDate); console.log('index: ', index); // this.tabIndex = index; this.initcurrentIndex();}}};</script>
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 何鸭丫 普通会员 1楼

      在将JavaScript代码转换为TypeScript时,有几个步骤可以参考:

      1. 首先,你需要将JavaScript代码从javascript文件或tsconfig.json文件导入到TypeScript项目中。

      2. 使用TypeScript的import语句将JavaScript代码导入到一个模块或函数中。

      3. 在JavaScript代码中,使用TypeScript的export语句将变量、函数、类等导出为类型更安全的类型。

      4. 在TypeScript中,你可以使用TypeScript的类型注释(TypeScript的//注释)来为变量、函数、类等添加类型信息。

      以下是一个简单的例子:

      ```typescript // 在JavaScript文件中 function add(a: number, b: number): number { return a + b; }

      // 在TypeScript文件中 export function add(a: number, b: number): number { return a + b; } ```

      在这个例子中,add函数在JavaScript文件中定义,然后在TypeScript文件中导出为add函数类型。这意味着在TypeScript项目中,你可以在调用add函数时指定参数的类型。例如:

      typescript const result = add(2, 3); console.log(result); // 输出 5

      这表示在TypeScript项目中,你可以使用参数的类型来保证函数的运行结果是正确的。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部