小编Ale*_*sky的帖子

无法弄清楚 vue-router 是如何工作的

我有一个带有以下路由器的 Vue.js 项目:

import Vue from 'vue';
import Router from 'vue-router';
import Overview from '@/components/Overview';
import Experiment from '@/components/ForExperiment';

Vue.use(Router);

export default new Router({
  routes: [
    {
      path: '/',
      redirect: 'test',
    },
    {
      path: '/overview',
      component: Overview,
    },
    {
      path: '/overview/from/:from/to/:to',
      name: 'overview',
      component: Overview,
    },
    //... some other urls goes here.
    {
      path: '/test',
      name: 'test',
      component: Experiment,
    },
  ],
});
Run Code Online (Sandbox Code Playgroud)

如果我http://localhost:8080在浏览器中打开,我将被重定向到http://localhost:8080/#/test. 为什么不只是http://localhost:8080/test?“#”符号从何而来?

为什么如果我打开http://localhost:8080/test我被重定向到http://localhost:8080/test#/test

更奇怪的是,如果我打开,http://localhost:8080/overview我会被重定向到http://localhost:8080/overview#/test,因此不会显示概览组件。 …

vue.js vue-router

3
推荐指数
1
解决办法
1758
查看次数

'if'、'while'、'catch' 等之后的空格

想不出该选项后,增加了空间ifwhilecatch,等...

目前我的 .clang 格式文件产生这个:

        while(true)
        {
            if(flushedCount == count)
            {
                break;
            }
        }
Run Code Online (Sandbox Code Playgroud)

c c++ clang-format

3
推荐指数
1
解决办法
953
查看次数

不能在 C++ 中使 std::array 成为 constexpr

在下面的代码中,我在 VS2017 中收到“错误 C3615:constexpr 函数 'to_array' 无法导致常量表达式”编译器错误:

#include <stdio.h>
#include <array>

template <typename T>
static constexpr std::array<std::uint8_t, sizeof(T)> to_array(T value)
{
    std::array<std::uint8_t, sizeof(T)> result {};

    for (std::size_t i{ sizeof(T) }; i != 0 ; --i)
    {
        result[i - 1] = static_cast<uint8_t>(value >> ((sizeof(T) - i) * 8));
    }

    return result;
}

int main()
{
    constexpr uint64_t sample = UINT64_C(0xab28ecb46814fe75);

    //error C3615: constexpr function 'to_array' cannot result in a constant expression
    constexpr auto a = to_array(sample);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果理论上std::array …

c++

2
推荐指数
1
解决办法
917
查看次数

Constexpr Lambda参数

是否可以使用带有constexpr参数的lambda?是否可以使以下示例正常工作?

ForEach 下面提供的函数用索引0、1、2调用给定的lambda 3次:

template <class Func, std::size_t... index>
inline constexpr void ForEach(Func && f, std::index_sequence<index...>)
{
    (f(index), ...);
}

template <class Func>
inline constexpr void ForEach(Func && f)
{
    ForEach(f, std::make_index_sequence<3>());
}
Run Code Online (Sandbox Code Playgroud)

所以下面的代码

ForEach([](size_t index)
{
    std::cout << index << ' ' << std::endl;
});
Run Code Online (Sandbox Code Playgroud)

输出0、1、2

但是以下尝试打印元组元素的代码index必须是constexpr:

auto t = std::make_tuple(1, 2.0, std::string("abc"));

ForEach([&t](size_t index)
{
    std::cout << std::get<index>(t) << ' ' << std::endl;
});
Run Code Online (Sandbox Code Playgroud)

因此无法编译,请参见在线示例。是否可以以index某种方式使constexpr?

EDIT1:有一个工作示例,其中lambda参数用作模板参数:

void Set(Tuple& val, …
Run Code Online (Sandbox Code Playgroud)

c++ c++17

2
推荐指数
1
解决办法
86
查看次数

找不到符号 GoogleApiAvailability

我从 Google Play Services 11.0.4 切换到 18.1.1 并在以下代码中收到编译器错误:

public boolean isGooglePlayServicesAvailable()
{
    com.google.android.gms.common.GoogleApiAvailability googleApiAvailability = com.google.android.gms.common.GoogleApiAvailability.getInstance();
    int resultCode = googleApiAvailability.isGooglePlayServicesAvailable(this);
    return resultCode == com.google.android.gms.common.ConnectionResult.SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

错误信息是:

MainActivity.java:246: error: cannot find symbol
        com.google.android.gms.common.GoogleApiAvailability googleApiAvailability = com.google.android.gms.common.GoogleApiAvailability.getInstance();
                                                                                                                 ^
  symbol:   class GoogleApiAvailability
  location: package com.google.android.gms.common
Run Code Online (Sandbox Code Playgroud)

Google Play Services 18.1.1 中是否提供GoogleApiAvailability

build.gradle我有这个:

apply plugin: 'com.android.application'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
    implementation 'com.google.android.gms:play-services-ads:18.1.1'
}
Run Code Online (Sandbox Code Playgroud)

编辑1:

com.google.android.gms.common.GoogleApiAvailabilityLight至少。

android google-play-services

2
推荐指数
2
解决办法
1536
查看次数

作为函数参数的引用元组

我不太明白为什么func下面代码中的第二次调用不起作用:

\n
#include <string>\n#include <iostream>\n#include <tuple>\n\nusing Tuple = std::tuple<const int&, const std::string&>;\n\nvoid func(const Tuple& t)\n{\n    std::cout << std::get<1u>(t) << std::endl;\n}\n\nint main()\n{\n    const int n = 3;\n    const std::string text = "xyz";\n    \n    func(Tuple(n, text));\n\n    //Does not work:\n    //func(Tuple(5, "abc"));\n\n    return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

临时字符串什么时候"abc"销毁?

\n

编辑1

\n

所以,最后,根据 \xe5\xba\xb7\xe6\xa1\x93\xe7\x91\x8b 这是有效的:

\n
    func(Tuple(5, std::string("abc")));\n
Run Code Online (Sandbox Code Playgroud)\n

但这并没有:

\n
    Tuple t(5, std::string("abc"));\n    func(t);\n
Run Code Online (Sandbox Code Playgroud)\n

正确的?如果是的话有什么区别?

\n

c++ temporary-objects c++20

2
推荐指数
1
解决办法
534
查看次数

如何检查一个类是否有特定的构造函数?

我有以下源代码:

std::string s;
std::string_view sv(s.begin(), s.end());
Run Code Online (Sandbox Code Playgroud)

它可以使用 MSVC 和 GCC 进行编译,但不能使用 Apple Clang 进行编译。

编写这样的函数的正确方法是什么

template< class It >
constexpr std::string_view make_string_view( It first, It last );
Run Code Online (Sandbox Code Playgroud)

是否可以使用 C++17 和 C++20 进行编译,并使用std::string_view接受两个迭代器的新构造函数(如果可能)?

c++ c++20

2
推荐指数
1
解决办法
100
查看次数

带有Vue.js的VS Code中Chrome的调试器设置

在.vscode \ launch.json中具有以下设置

{
    "version": "0.2.0",
    "configurations": [
      {
        "type": "chrome",
        "request": "launch",
        "name": "Launch Chrome against localhost",
        "url": "http://localhost:8080",
        "webRoot": "${workspaceFolder}"
      }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我可以在项目的index.js文件中设置一个断点,并且触发成功,但是* .vue文件中的断点触发不正确。

使用以下设置:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "chrome",
      "request": "launch",
      "name": "vuejs: chrome",
      "url": "http://localhost:8080",
      "webRoot": "${workspaceFolder}/src",
      "breakOnLoad": true,
      "sourceMapPathOverrides": {
        "webpack:///src/*": "${webRoot}/*"
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

反之亦然,*。vue文件中的断点已成功触发,但是我无法在index.js文件中设置断点。

如何使断点在* .js和* .vue中都能工作?

查看有关如何设置环境的更多详细信息。

vue.js visual-studio-code

1
推荐指数
1
解决办法
567
查看次数

在Vue.js应用中的哪里调用axios.interceptors.request.use?

我正在尝试Amazon Cognito Vuex Module在我的Vue.js应用中使用,并axios使用以下代码使所有请求自动通过凭据:

// Add authentication token to each request
axios.interceptors.request.use(async config => {
    const response = await store.dispatch('getUserSession');
if (response && response.accessToken && response.accessToken.jwtToken) {
    config.headers.AccessToken = response.accessToken.jwtToken;
}
    return config;
});
Run Code Online (Sandbox Code Playgroud)

据我所知,这是可能应该对所有组件执行的通用代码,但尚不清楚在何处添加它。大概到App.vue还是到index.js?在App.vue我有:

import Vue from 'vue';
import VueRouter from 'vue-router';
import Vuetify from 'vuetify';

    Vue.use(Vuetify);
    Vue.use(VueRouter);

export default new Vue({}).$mount('#app');
Run Code Online (Sandbox Code Playgroud)

index.js

export default new Vuex.Store({
  state: {
  ...
Run Code Online (Sandbox Code Playgroud)

javascript vue.js amazon-cognito axios vuejs2

1
推荐指数
1
解决办法
1448
查看次数

VS2017是否完全支持折叠表达式?

我找到了一篇有趣的文章并尝试了MSVS 2017的代码:

#include <utility>
#include <tuple>

template <typename... Args, typename Func, std::size_t... Idx>
void for_each(const std::tuple<Args...>& t, Func&& f, std::index_sequence<Idx...>) {
    f(std::get<Idx>(t))...;
}

template <typename... Args, typename Func>
void for_each(const std::tuple<Args...>& t, Func&& f) {
    for_each(t, f, std::index_sequence_for<Args...>{});
}

template <typename T>
void Write(std::wostream & out, const T & t)
{
    out << t;
}

template<typename ...Args>
void WriteV(std::wostream & out, Args&... args)
{
    for_each(std::tuple<Args&...>(args...), [&out](auto& a) { Write(out, a); });
}

struct A
{
    int n;
    std::wstring …
Run Code Online (Sandbox Code Playgroud)

c++ template-meta-programming variadic-templates fold-expression c++17

1
推荐指数
1
解决办法
139
查看次数