如何用const替换字符串的一部分?

时间:2018-06-19 18:17:31

标签: javascript vue.js

那是我的代码:

const searchTerm = `?term=${this.term}`;
const routeWithoutTerm = this.$route.fullPath.replace(searchTerm , '');

我想从searchTerm中删除this.$route.fullPath并将其保存在routeWithoutTerm中,但这显然是错误的方法。它什么也不会替代。我该如何实现?

编辑:只能说在执行this.term = 'help'this.$route.fullPath = 'search/help?term=help'时,我想删除?term=help

2 个答案:

答案 0 :(得分:0)

我终于找到了问题。

我的searchTerm中有变音符(ö,ä,ü),所以我必须先在encodeURI()上使用searchTerm

答案 1 :(得分:-1)

使用substring()函数获取所需零件,并使用replace()将其替换为所需零件

import React from "react";

const EyewearPurchaseBtn = (props) => (
    <div>
        <form className="eyewear-purchase-form" onSubmit={props.handleSubmit}>
            <h3>Select Size</h3>

                    {
                        props.sizes.map( size => (
                            <div key={size} >
                            <input onChange={props.handleChange} className="radio-btn" type="radio" name="purchaseVal" value={size} />
                            <label className="eyewear-purchase-label">{size}</label><br/>   
                            </div>
                        ))
                    }

                    <button className="buy-now">Buy Now</button>    
                </form>
    </div>
);

export default EyewearPurchaseBtn;